文本字段未在NSNotification中更新

时间:2012-11-01 06:05:58

标签: iphone ios nsnotificationcenter nsnotification

我正在对NSNotification做一个简单的教程,并在正确执行它时面临一些问题。当我单击第一个视图上的按钮时,第二个视图中的文本字段不会更新。当我在NSLog中放置receiveNotification进行调试时,没有回复。不确定为什么没有调用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

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

2 个答案:

答案 0 :(得分:1)

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中

@interface SecondViewController()

@end

@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];
}

答案 1 :(得分:0)

不是@selector(receiveNotification),应为@selector(receiveNotification:)