我尝试将数据从一个ViewController传递给secondControler,但是似乎不起作用。我使用NSNotification。 -2个控制器具有相同的类“ ViewController”
在viewcontroller.m
- (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];
}
运行程序并按按钮时,根本没有更新的标签文本。您知道为什么以及如何解决。
新代码: 在SecondViewController.m
@interface SencondViewController ()
@end
@implementation SencondViewController
@synthesize progressValue;
@synthesize labelView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
labelView.stringValue =progressValue;
}
在FirstViewCOntroller中:
- (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(测试)和其他按钮2(测试2)始终显示具有更新值的新视图。我只需要显示1个视图。
答案 0 :(得分:1)
为什么需要使用nsnotification,最简单的方法是使用prepareForSegue或委托
这是一个例子
#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 :(得分:1)
不建议这样做的通知模式。使用通知 当您想将某些事件的数据传递给多个对象时。
要解决此问题:
步骤1: 您应该将View Controller名称更改为FirstViewController和SecondViewController,并在SecondViewController中声明一个属性,该属性要从FirstViewController设置。
步骤2: 最后,在FirstViewController的“准备Segue”方法中,设置数据。
在Objective-C中,您可以尝试以下代码:
#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的头文件中,声明属性:
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property (nonatomic,strong) NSString *someValue;
@end
在SecondViewController的实现文件中,编写:
#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