目标C 基本问题

发布于 2024-09-15 17:36:43 字数 356 浏览 2 评论 0原文

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

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

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

i made a simple application using view based template.and i put only nslog inside view didload method in viewController file and also inside applicationDidFinishLaunch method (in appDelegate )to checked which class file called first.

after the run i got: viewController Run first and then appdelegate ..but i think appdelegate should first then other's called according to the need ... plz give me the proper reasion.

Noted that --i did not call viewController (didnot make object) in my appDelegate(inside application didFinishLaunch) . i am using ios4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我闹别瞎闹 2024-09-22 17:36:43

如果您的视图控制器是 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 以获得您期望的结果。以下是两个示例片段,其中包含您期望语句加载的方式。

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

If your View Controller is a property of the AppDelegate, similar to the code reference

@interface AppDelegate_Shared : NSObject <UIApplicationDelegate, UIAlertViewDelegate, OMFDataLoadDelegate> {

    NSManagedObjectModel *managedObjectModel;
    NSManagedObjectContext *managedObjectContext;       
    NSPersistentStoreCoordinator *persistentStoreCoordinator;

    UIWindow *window;

    UITabBarController *tabBarController;

}

then it is probably getting allocated by the AppDelegate when it is being allocated. According to the Apple documentation viewDidLoad is run after the view is loaded into memory, which can be a little confusing, since the language can make you believe it's when it's loaded onto the screen.

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

Move your NSLog statement to viewDidAppear for the result you were expecting. Here's two sample snippets with the way you should expect the statements to load.

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;
}
看透却不说透 2024-09-22 17:36:43

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

消息按正确的顺序发送。

If the initial view hasn't loaded then clearly the application has not finished launching.

The messages are sent in the right order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文