小编Der*_*rek的帖子

带有NSNotification的removeObserver ...我做错了什么?

基本上,我有一个view1,在某些时候,调用view2(via presentModalViewController:animated:).当UIButton按下某个视图2时,view2在view1中调用一个通知方法,然后立即关闭.通知方法会弹出警报.

通知方法工作正常,并被适当调用.问题是,每次创建view1时(一次只能存在一个view1),我可能会NSNotification创建另一个view1因为如果我从view0(菜单)转到view1,然后来回几次,我得到一个一系列相同的警报消息,一个接一个地从通知方法中多次打开一个view1.

这是我的代码,请告诉我我做错了什么:

View1.m

-(void) viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(showAlert:) 
                                                 name:@"alert" 
                                               object:nil];
}

-(void) showAlert:(NSNotification*)notification {
    // (I've also tried to swap the removeObserver method from dealloc
    // to here, but it still fails to remove the observer.)
    // < UIAlertView code to pop up a message here. >
}

-(void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}
Run Code Online (Sandbox Code Playgroud)

View2.m

-(IBAction) buttonWasTapped {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"alert" 
                                                        object:nil];
    [self dismissModalViewControllerAnimated:YES];
}

-(void) dealloc { …
Run Code Online (Sandbox Code Playgroud)

objective-c nsnotification ios

28
推荐指数
2
解决办法
5万
查看次数

UIImageView startAnimating:你如何让它停止在系列的最后一张图片上?

我正在使用UIImageView创建一个简单的动画,一切都很完美,但是当我使用startAnimating时,动画会运行一次(我希望它),但是回到第一帧.我希望动画停在最后一帧.

我用:

myAnimation.animationImages = myArray;
myAnimation.animationDuration = 0.7;
myAnimation.animationRepeatCount = 1;
[myAnimation startAnimating];
Run Code Online (Sandbox Code Playgroud)

我可以使用覆盖startAnimating方法吗?解决问题的最佳方法是什么?

iphone objective-c

3
推荐指数
1
解决办法
3863
查看次数

标签 统计

objective-c ×2

ios ×1

iphone ×1

nsnotification ×1