为什么多次调用NSNotification并且未显示视图?

时间:2017-02-27 01:59:18

标签: ios objective-c nsnotificationcenter nsnotifications

我制作了一个ViewController并设置了NSNotification。 如果我按下按钮,将出现具有不同标签的相同ViewController,另一个视图(子类)也将出现。 但是,即使我删除了NSNotification,NSNotification的调用次数也会被多次调用。

如果我更改了编写NSNotification的位置,则视图(子类)没有出现,有时视图很快就会出现。 (它应该在返回ViewController之前1秒后出现)

为什么以及如何解决?

@implementation aViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImageView *bgimgV = [[UIImageView alloc]init];
    bgimgV.image = [UIImage imageNamed:@"background.png"];
    bgimgV.frame = self.view.bounds;
    [self.view addSubview:bgimgV];

    UIButton* backBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    UIImage *backBtnimg = [UIImage imageNamed:@"button.png"];
    [backBtn setBackgroundImage:backBtnimg forState:UIControlStateNormal];
    backBtn.frame = CGRectMake(0, 5, 50, 50);
    [backBtn addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];

    UILabel* questLbl = [[UILabel alloc]init];
    questLbl.frame = CGRectMake(10, 45, 300, 180);

    NSString *string = [NSString stringWithFormat:@"%@",set[0]];
    string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@"\n"];
    questLbl.text = string;
    questLbl.textColor = RGB(10, 10, 10);
    questLbl.font =  [UIFont fontWithName:@"HiraKakuProN-W6" size:15];
    questLbl.numberOfLines = 0;
    [self.view addSubview:questLbl];

    for(int b = 0; b<3; b++){
        answerBtn[b] = [UIButton buttonWithType:UIButtonTypeCustom];
        answerBtn[b].frame = CGRectMake(self.view.frame.size.width/2-(576/2)/2, 290+b*65, 576/2, 90/2);

        [answerBtn[b] setImage:[UIImage imageNamed
                               :[NSString stringWithFormat:@"answer%d.png",b+1]] forState:UIControlStateNormal];
        answerBtn[b].tag = b;
        [answerBtn[b] addTarget:self action:@selector(answerBtn BtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview: answerBtn[b]];
     }

     [[NSNotificationCenter defaultCenter]addObserver:self
                                             selector:@selector(addView)
                                                 name:@"addView"
                                               object:nil];

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:@"addView"
                                                  object:nil];
}

//when press the back button, add view (sub class)
-(void)backBtnAction:(UIButton*)button{

    if([APP_DELEGATE questTag] == 0){

    }else{
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        appDelegate.questionTag--;
    }

    [self.navigationController popViewControllerAnimated:YES];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"addView"
                                                        object:nil];

}

//when press answer button, same ViewController will appear and change the label
-(void)answerBtnAction:(UIButton*)button{

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.questTag++;

    double delayInSeconds = 0.3;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
           [self changePage];
    });

}

-(void)changePage{

    aViewController *aVC = [self.storyboard instantiateViewControllerWithIdentifier:@"aVC"];
    [self.navigationController pushViewController:aVC animated:YES];

}

//After back to before page, view (subclass) will be appear after 1 second
-(void)addView{

    [self performSelector:@selector(viewAppear) withObject:nil afterDelay:1.0];

}

-(void)viewAppear{

   self.RecView = [[InsteRecView alloc] initWithFrame:CGRectMake(0, 0,    [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        [self.view addSubview:self.RecView];

}

0 个答案:

没有答案