【问题标题】:objective C Basic question目标 C 基本问题
【发布时间】:2010-08-20 06:05:10
【问题描述】:

我使用基于视图的模板制作了一个简单的应用程序。我只将 nslog 放入 viewController 文件中的视图 didload 方法和 applicationDidFinishLaunch 方法(在 appDelegate 中)以检查首先调用的类文件。

运行后我得到:viewController 先运行,然后是 appdelegate ..但我认为 appdelegate 应该首先根据需要调用其他的......请给我正确的理由。

注意到 --i 没有在我的 appDelegate(inside application didFinishLaunch) 中调用 viewController(没有创建对象)。我正在使用ios4

【问题讨论】:

    标签: objective-c uiviewcontroller uiapplicationdelegate uiapplication


    【解决方案1】:

    如果你的ViewController是AppDelegate的属性,类似代码参考

    @interface AppDelegate_Shared : NSObject <UIApplicationDelegate, UIAlertViewDelegate, OMFDataLoadDelegate> {
    
        NSManagedObjectModel *managedObjectModel;
        NSManagedObjectContext *managedObjectContext;       
        NSPersistentStoreCoordinator *persistentStoreCoordinator;
    
        UIWindow *window;
    
        UITabBarController *tabBarController;
    
    }
    

    那么它可能在分配时由 AppDelegate 分配。根据 Apple 文档,viewDidLoad 是在视图加载到内存后运行的,这可能有点令人困惑,因为该语言可以让您相信它是在加载到屏幕上时发生的。

    http://developer.apple.com/iphone/library/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW25

    将您的 NSLog 语句移动到 viewDidAppear 以获得您期望的结果。这是两个示例 sn-ps,您应该期望语句加载的方式。

    ViewController.m

    - (void) viewDidLoad {
      NSLog(@"1st - this occurs when appDelegate allocates this object");
    }
    - (void) viewDidAppear {
    
      NSLog(@"3rd - this should appear after the applicationDidFinishLaunchingStatement");
    }
    

    AppDelegate_Shared.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
        NSLog(@"2. Starting AppDelegate_Shared");
    
        [window addSubview:self.tabBarController.view];
        [window makeKeyAndVisible];
    
        NSLog(@"4. Leaving AppDelegate_Shared");
        return YES;
    }
    

    【讨论】:

    • 感谢 Travish 为您提供 ..我应用了您的建议,但顺序是 1、2、4、3。我的问题是“代码流是什么?”我首先阅读文档'main.m'调用'appdelegate class',然后是我们在applicationDidFinishLaunch方法中编写的其他类......你能解释一下......
    【解决方案2】:

    如果初始视图尚未加载,则显然应用程序尚未完成启动。

    消息以正确的顺序发送。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-14
      • 2018-03-19
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多