Iphone 在AppDelegate中使用自定义委托

Iphone 在AppDelegate中使用自定义委托,iphone,ios,delegates,appdelegate,Iphone,Ios,Delegates,Appdelegate,我正在尝试在App Delegate中添加一个自定义委托,我是这样做的: AppDelegate.h @protocol AppDelegateDelegate <NSObject> - (void)finishSync:(BOOL)success; @end @interface AppDelegate : UIResponder <UIApplicationDelegate> { @property (nonatomic, weak) id <AppD

我正在尝试在App Delegate中添加一个自定义委托,我是这样做的:

AppDelegate.h

@protocol AppDelegateDelegate <NSObject>

- (void)finishSync:(BOOL)success;

@end

@interface AppDelegate : UIResponder <UIApplicationDelegate> {

@property (nonatomic, weak) id <AppDelegateDelegate> delegate;

@end
FirstView.h

#import "AppDelegate.h"

@interface FirstView : UIViewController <AppDelegateDelegate>

@end

FirstView.m

@implementation FirstView

...

- (void)viewDidLoad {
  AppDelegate *appController = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  appController.customDelegate = self;

}
我切换第三个视图:

(null)
<ThirdView: 0xab297e0>
(null)
第二点:

(null)
第四:

(null)
不再工作,因此我停止应用程序并再次运行它,然后从firstView开始:

<FirstView: 0xad28730>
(null)
切换到第二视图:

<SecondView: 0x9f682e0>
(null)
切换到第三视图:

(null)
<ThirdView: 0xab297e0>
(null)
正如您所看到的,问题是第一次工作,然后当返回视图时,委托为空,您知道吗

编辑2:


我还注意到,如果我在NavigationController之间切换视图,委托永远不会(null),而是像我一样在UITabBarController中切换视图,我的代码会给出null…因此,UITabBarController视图的问题我认为…

一个对象一次只能有一个委托。尝试在FirstView的ViewWillEnglishe方法中将委托设置为nil,看看这是否有帮助

这对我有用。我将代理的设置移动到第一个控制器中的ViewWillDisplay,因此如果从第二个控制器返回,它将重置。我的控制器在导航控制器中,我使用推送和弹出按钮来回移动。两个控制器具有相同的代码(日志除外):


对于一个对象,一次只能有一个委托是什么意思?FirstView一次只能有一个委托或AppDelegate一次只能有一个委托?AppDelegate。AppDelegate是具有委托的,而不是FirstView(FirstView是委托)。好的,谢谢,但是对于视图中的write delegate=nil将消失,我必须在头文件中声明appController。对吗?或者我可以这样做:AppDelegate*appController=(AppDelegate*)[[UIApplication sharedApplication]delegate];appController.customDelegate=nil;在视野中会消失吗?最好的方法是什么?@Piero,你已经用第二种方法设置了委托,所以用同样的方法将其设置为nil(或者更好的方法是,将appController设置为属性,然后你可以在ViewWillEnglishe方法中重用它)。不行,也许我必须在ViewWillEnglish中添加委托,而不是在视图中加载你认为的?
<ThirdView: 0xab297e0>
<FourthView: 0xab28430>
(null)
#import "ViewController.h"
#import "AppDelegate.h"

@interface ViewController ()
@property (strong,nonatomic) AppDelegate *appController;
@end

@implementation ViewController //This is the first controller


-(void)viewDidAppear:(BOOL)animated {
    self.appController = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    self.appController.customDelegate = self;
    [super viewDidAppear:animated];
    NSLog(@"First says: %@",self.appController.customDelegate);
}


-(void)viewWillDisappear:(BOOL)animated {
    NSLog(@"First viewWillDisappear");
    [super viewWillDisappear:animated];
    self.appController.customDelegate = nil;
}

- (void)finishSync:(BOOL)success {

}