gpt4 book ai didi

iphone - NSNotification 中的文本字段未更新

转载 作者:行者123 更新时间:2023-11-29 13:25:48 28 4
gpt4 key购买 nike

我正在做一个关于 NSNotification 的简单教程,但在正确执行它时遇到了一些问题。当我单击第一个 View 上的按钮时,第二个 View 中的文本字段没有得到更新。当我通过在 receiveNotification 中放置一个 NSLog 进行调试时,没有响应。不确定为什么未调用 receiveNotification

代码如下所示:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

}
-(IBAction)one:(id)sender;
-(IBAction)two:(id)sender;
-(IBAction)secondview:(id)sender;
@end

ViewController.m

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)one:(id)sender{
[[NSNotificationCenter defaultCenter] postNotificationName:@"Test1" object:self];
NSLog(@"Hello");
}
-(IBAction)two:(id)sender{
[[NSNotificationCenter defaultCenter] postNotificationName:@"Test2" object:self];
NSLog(@"Hello");
}
-(IBAction)secondview:(id)sender{
SecondViewController *secondview = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self presentViewController:secondview animated:YES completion:nil];
}

@end

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController{
IBOutlet UITextField *counterOneText;
IBOutlet UITextField *counterTwoText;
int counterOne;
int counterTwo;
}

-(IBAction)back:(id)sender;
@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

counterOne = 0;
counterTwo = 0;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification) name:@"Test1" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification) name:@"Test2" object:nil];
}
return self;
}

-(void)receiveNotification:(NSNotification *)notification{
if([[notification name] isEqualToString:@"Test1"]) {
counterOne++;
counterOneText.text = [NSString stringWithFormat:@"%d",counterOne];
NSLog(@"%@",counterOneText.text);
NSLog(@"Hello");
}else
{
counterTwo++;
counterTwoText.text = [NSString stringWithFormat:@"%d",counterTwo];
}
}

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

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)back:(id)sender{
[self dismissViewControllerAnimated:YES completion:nil];
}

@end

需要一些指导......

最佳答案

receiveNotification 未被调用,因为您的 SecondViewController 从未实例化。当对象不存在时无法调用它。如果您按下 secondViewback,然后单击 onetwo,则会触发 receiveNotification。

编辑:我复制并粘贴了你的代码来测试它,我得到了你想要它做的大部分事情。您仍然需要先按 secondview,然后按 back

在 ViewController.m 中

@interface ViewController (){
SecondViewController *secondview;
}

@end

@implementation ViewController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName: nibNameOrNil bundle:nibBundleOrNil];
if(self){
secondview = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)one:(id)sender{
[[NSNotificationCenter defaultCenter] postNotificationName:@"Test1" object:self];
}
-(IBAction)two:(id)sender{
[[NSNotificationCenter defaultCenter] postNotificationName:@"Test2" object:self];
}
-(IBAction)secondview:(id)sender{
[self presentViewController:secondview animated:YES completion:nil];
}

在SecondViewController.h中

@interface SecondViewController : UIViewController{
int counterOne;
int counterTwo;
}

@property (nonatomic,retain)UITextField *counterOneText;

@property (nonatomic,retain)UITextField *counterTwoText;


-(IBAction)back:(id)sender;

@end

在SecondViewController.m中

@interfaceSecondViewController()

@结束

@implementation SecondViewController
@synthesize counterOneText, counterTwoText;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

counterOne = 0;
counterTwo = 0;

self.counterOneText = [[UITextField alloc]initWithFrame:CGRectMake(100, 50, 150, 30)];
self.counterOneText.borderStyle = UITextBorderStyleRoundedRect;
self.counterOneText.text = [NSString stringWithFormat:@"%@", [NSNumber numberWithInt:counterOne]];

self.counterTwoText = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];
self.counterTwoText.borderStyle = UITextBorderStyleRoundedRect;
self.counterTwoText.text = [NSString stringWithFormat:@"%@", [NSNumber numberWithInt:counterTwo]];

//self.counterOneText.text = [NSString stringWithFormat:@"%d",counterOne];
//self.counterTwoText.text = [NSString stringWithFormat:@"%d",counterTwo];


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"Test1" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"Test2" object:nil];
}
return self;
}

-(void)receiveNotification:(NSNotification *)notification{
if([[notification name] isEqualToString:@"Test1"]) {
counterOne++;
NSLog(@"counterOne: %d", counterOne);
counterOneText.text = [NSString stringWithFormat:@"%@", [NSNumber numberWithInt:counterOne]];

NSLog(@"counterOneText.text: %@",counterOneText.text);
NSLog(@"Test1");
}else
{
counterTwo++;
NSLog(@"counterTwo: %d", counterTwo);
counterTwoText.text = [NSString stringWithFormat:@"%@",[NSNumber numberWithInt:counterTwo]];

NSLog(@"counterTwoText.text: %@",counterTwoText.text);
NSLog(@"Test2");


}

}

- (void)viewDidLoad
{
[self.view addSubview:self.counterOneText];
[self.view addSubview:self.counterTwoText];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)back:(id)sender{
[self dismissViewControllerAnimated:YES completion:nil];
}

关于iphone - NSNotification 中的文本字段未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13171805/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com