Xcode:视图控制器和应用程序代理之间的连接 [英] Xcode: Connection Between View Controllers and App Delegate

查看:81
本文介绍了Xcode:视图控制器和应用程序代理之间的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个noob的问题,但不能让我的头。

This is probably a noob question but can't get my head around it.

如何使2视图控制器之间的连接或视图控制器和我的appdelegate ?
我通常做的是添加以下到我的应用程序委托h文件

How do i make a connection between 2 viewcontrollers or a view controller and my appdelegate? what i usually do is add the following to my app delegate "h" file

@class RootViewController;


@interface TabBarWithSplitViewAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
    RootViewController *rootViewController;



}
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;

@end

,然后在Interface Builder中创建连接。从我的根视图控制器到应用程序委托,并自动告诉我的上面添加的rootViewController。

and then create a connection in the Interface Builder. from my root view controller to the app delegate and automatically tells me thats the rootViewController that i added above.

如果你这样做在应用程序委托m文件:

and if you do this on the app delegate "m" file:

#import "RootViewController.h"

NSLOG(@"Controller %@",rootViewController);

它给你一串数字,表示有一个连接

it gives you a bunch of numbers indicating that there is a connection

但是你知道xcode 4这个更改,因为你通常不再有main.xib,你可以创建连接,你几乎所有的连接编程。

But as you know with xcode 4 this changed since you usually no longer have the main.xib where you can create the connection, you do almost all those connections programatically.

i`ve尝试使用相同的代码而不使用IBOutlet添加:

i`ve tried everything from using the same code without the "IBOutlet" to adding:

rootViewController = [[RootViewController]alloc] init;

但没有任何效果。

提前感谢

推荐答案

基本上想在您的应用程序委托中创建一个视图控制器的ivar。

You will basically want to create an ivar of your view controller in your app delegate.

ViewController *myVC;
@property (nonatomic, retain) IBOutlet ViewController *myVC;

然后在实现文件中合成它。

then synthesize it in the implementation file.

然后当视图控制器加载时,调用以下行的东西:

Then when the view controller loads, call something along the lines of this:

- (void)viewDidLoad {
    AppDelegateClass *appDelegate = (AppDelegateClass *)[[UIApplication sharedApplication] delegate];
    appDelegate.myVC = self;
}



此时,您现在可以直接连接到视图控制器应用程序代理。同样,你可以从视图控制器调用app delegate方法。在这种情况下,您将在视图控制器的头中设置一个委托。

At this point, you now have a direct connection to your view controller from the app delegate. Similarly, you could do the opposite to call app delegate methods from the view controller. In that case, you'd set up a delegate in the view controller's header.

id delegate;
@property (nonatomic, assign) id delegate;

再次在实现文件中合成它。

again synthesizing it in the implementation file.

现在,当您在 viewDidLoad 中时,您可以这样调用:

Now when you are in viewDidLoad, you'd call something like this:

- (void)viewDidLoad {
    self.delegate = (AppDelegateClass *)[[UIApplication sharedApplication] delegate];
}

这应该给你你需要去的,所以我希望有帮助

That should give you what you need to get going, so I hope that helps

这篇关于Xcode:视图控制器和应用程序代理之间的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆