首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何继续将值从一个ViewController传递到第二个Viewcontroller并自动关闭、解除第二个ViewController

如何继续将值从一个ViewController传递到第二个Viewcontroller并自动关闭、解除第二个ViewController
EN

Stack Overflow用户
提问于 2018-08-22 23:47:22
回答 2查看 58关注 0票数 0

我尝试将数据从一个ViewController传递到secondControler,但似乎不起作用。我使用NSNotification。控制器具有相同的类别-2\f25 "ViewController“-2\f6

在视图控制器中。m

代码语言:javascript
复制
- (void)viewDidLoad {
       [super viewDidLoad];
       [[NSNotificationCenter defaultCenter] addObserver:self 
        selector:@selector(ProcessBarLoading) name:@"buttonPressed" object:nil];
   }
-(void)ProcessBarLoading{
      _labelTest.stringValue = @"TESTING";
    }
- (IBAction)test:(id)sender {
     [[NSNotificationCenter defaultCenter] postNotificationName:@"buttonPressed" object:self];
      NSStoryboard *storyboard = [NSStoryboard storyboardWithName:@"Main" bundle: nil];
      NSViewController * vc = [storyboard instantiateControllerWithIdentifier:@"SheetViewController"];    
[self presentViewControllerAsSheet:vc];
      }

当运行程序并按下按钮时,根本没有更新标签文本。你知道为什么我能修复。

新代码:在secudViewController.m中

代码语言:javascript
复制
@interface SencondViewController ()

@end

@implementation SencondViewController
@synthesize progressValue;
@synthesize labelView;
- (void)viewDidLoad {
 [super viewDidLoad];
 // Do view setup here.
 labelView.stringValue =progressValue;
 }

在FirstViewCOntroller中:

代码语言:javascript
复制
- (IBAction)test:(id)sender {
     self->uide = @"0";
     [self performSegueWithIdentifier:@"showRecipeDetail" sender:self->uide];
NSStoryboard *storyboard = [NSStoryboard storyboardWithName:@"Main" bundle: nil];

NSViewController * vc = [storyboard instantiateControllerWithIdentifier:@"SheetViewController"];
[self presentViewControllerAsSheet:vc];

- (void)prepareForSegue:(NSStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
SencondViewController * secondVC = segue.destinationController;
secondVC.progressValue = uide;
}  
}
- (IBAction)test2:(id)sender {

uide = @"80";
[self performSegueWithIdentifier:@"showRecipeDetail" sender:uide];
[self.view displayIfNeeded];
}

因此,无论我是否按下按钮1(测试),其他Button2 (test2)总是显示具有更新值新视图。我需要的是只显示一个视图。

EN

回答 2

Stack Overflow用户

发布于 2018-08-23 00:22:01

为什么需要使用prepareForSegue最简单的方法是使用nsnotification或委派

这是一个示例

代码语言:javascript
复制
    #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
        if ([segue.identifier isEqualToString:@"myId"]) {
            SecondViewController *vc = segue.destinationViewController;
            vc.myDataToPass = self.myValueInMyFirstViewController;
        }
}
票数 1
EN

Stack Overflow用户

发布于 2018-08-23 00:39:34

不推荐使用

通知模式来执行此操作。当您想在某个事件上将某些数据传递给多个对象时,请使用通知。

要解决此问题,请执行以下操作:

步骤1:您应该将视图控制器名称更改为FirstViewController和SecondViewController,并在SecondViewController中声明一个要从FirstViewController设置其值的属性。

第2步:分段最后,在FirstViewController的prepare 方法中设置数据。

在Objective-C中,您可以尝试以下代码:

代码语言:javascript
复制
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

//This will trigger the prepareForSegue method

-(IBAction) someButtonClick {
    [self performSegueWithIdentifier:@"YourSequeId" sender:nil];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    SecondViewController * secondVC = segue.destinationViewController;
    secondVC.someValue = @"PassYourValueHere";

}



@end

并在SecondViewController的头文件中声明属性:

代码语言:javascript
复制
#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property (nonatomic,strong) NSString *someValue;

@end

在SecondViewController的实现文件中,写入:

代码语言:javascript
复制
#import "SecondViewController.h"

@interface SecondViewController ()

@property (nonatomic,weak) IBOutlet UITextField *yourTextField;
@end

@implementation SecondViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    self.yourTextField.text = self.someValue

    // Do any additional setup after loading the view.
}



@end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51970601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档