简体   繁体   English

Xcode4,iOS:实例方法的某些部分被忽略,没有错误,只是通过

[英]Xcode4, iOS: Certain parts of instance method being ignored, no errors, just passed by

new iOS guy here. 新的iOS家伙在这里。 I have a problem that Googling and searching on here has not shed any light on. 我有一个问题,即在Google上进行谷歌搜索和搜索还没有发现任何问题。 I'm assuming this is basic. 我假设这是基本的。 I have a simple app (app delegate and 1 view controller), and as part of it I'm using local notification. 我有一个简单的应用程序(应用程序委托和1个视图控制器),作为其一部分,我使用了本地通知。 So, in the app delegate I use the 'didReceiveLocalNotification' to watch for the notifications. 因此,在应用程序委托中,我使用'didReceiveLocalNotification'来监视通知。 Depending on which one comes in, I then call one of several instance methods in my main view controller. 根据进入哪个实例,然后在主视图控制器中调用几种实例方法之一。

ie in the AppDelegate.m 即在AppDelegate.m中

- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification {
   MyViewController* controller = [[MyViewController alloc] autorelease];
   if ([[notification.userInfo objectForKey:@"id"] isEqualToString:@"myKey"]) {
     [controller checkActive];
   }
 }

Through logging and watching some breakpoints, this is all working. 通过记录并查看一些断点,一切正常。 If the app is in the background, the notification comes in, app opens, and the correct instance method is called. 如果应用程序在后台运行,则将出现通知,应用程序将打开,并调用正确的实例方法。

What I cannot figure out at all is why some parts of the instance method are simply being passed by, with no effect. 我根本无法弄清楚的是为什么实例方法的某些部分只是简单地通过而没有任何效果。 For a simple example, if we have this: 举一个简单的例子,如果我们有这个:

-(void)checkActive {
    ViewThing.alpha = 1.0;
    NSLog(@"checkActive ran");
}

The log statement will show up fine, but the ViewThing will not change. 日志语句将正常显示,但ViewThing不会更改。 Elsewhere in the main view controller I'm calling the same checkActive method with no problems and it changes the ViewThing. 在主视图控制器的其他地方,我没有问题地调用了相同的checkActive方法,并且它更改了ViewThing。 (via another interface button IBAction method in that case). (在这种情况下,通过另一个接口按钮IBAction方法)。

There are no errors, no warnings, the console shows nothing, putting a breakpoint on ViewThing shows that it hits the line. 没有错误,没有警告,控制台什么也不显示,在ViewThing上放置一个断点表明它成功了。 I'm stumped, cannot see what is different from trying to calling the method from the delegate vs. on an IBAction. 我很困惑,看不到与尝试从委托与在IBAction上调用方法有什么不同。

Thanks for any tips! 感谢您的提示!

If the alpha is not correctly changing there a few possible issues with 1 and 2 being the most likely. 如果alpha值未正确更改,则可能会出现一些问题,最有可能出现1和2的问题。

  1. ViewThing is nil . ViewThing nil Reasons could be is the view unloaded and you set it to nil or checkActive was called before the outlets were set. 原因可能是视图已卸载,您将其设置为nil或在设置出口之前调用了checkActive。

  2. ViewThing.alpha is being set on a thread that is not the main thread. 在不是主线程的线程上设置了ViewThing.alpha Attempting to change UI elements on a separate thread will caused undefined behavior such as never updating the change or taking an extended amount of time to update the change. 尝试在单独的线程上更改UI元素将导致未定义的行为,例如从不更新更改或花费较长的时间来更新更改。 You can check if it is the main thread using [NSThread isMainThread] . 您可以使用[NSThread isMainThread]检查它是否为主线程。

  3. ViewThing is pointing a different view. ViewThing指向不同的视图。

1 & 2 can easily be checked by logging view 通过记录视图可以轻松检查1和2

NSLog(@"checkActive ran %@", ViewThing);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM