简体   繁体   English

从Appdelegate触发ViewController中的委托方法

[英]Triggering delegate method in ViewController from Appdelegate

I'm trying to use the AppDelegate to trigger a delegate method in the viewcontroller via and NSTimer. 我正在尝试使用AppDelegate通过NSTimer在viewcontroller中触发委托方法。 So in the AppDelegate, I basically have: 因此,在AppDelegate中,我基本上有:

AppDelegate.h AppDelegate.h

@protocol TestDelegate <NSObject>

-(void)testFunction:(NSString *)testString;

@end

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) id<TestDelegate> testDelegate;
...
@end

AppDelegate.m AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    [self.window makeKeyAndVisible];


    self.timer = [NSTimer scheduledTimerWithTimeInterval:10.0
                                                  target:self
                                                selector:@selector(trigger:)
                                                userInfo:nil
                                                 repeats:YES];
    return YES;
}

-(void)trigger:(id)sender {
   [self.testDelegate testFunction:self];
}

In my view controller I have: 在我的视图控制器中,我有:

ViewController.h ViewController.h

@interface ViewController : UIViewController <TestDelegate>
    @property (nonatomic, strong) AppDelegate *appDelegate;
@end

ViewController.m ViewController.m

@implementation ViewController
   ...
   -(void)viewDidLoad {
       self.appDelegate = [[UIApplication sharedApplication] delegate];
       self.appDelegate.testDelegate = self;
   }

   -(void)testfunction:(NSString *)testString {
       NSLog(@"%@", testString);
   }
@end

when I load the ViewController in my app, nothing happens? 当我在应用程序中加载ViewController时,什么也没发生? I know the NSTimer is successfully firing, but the delegate method isn't being triggered. 我知道NSTimer已成功触发,但未触发委托方法。

Where do you assign your VC as delegate? 您在哪里将VC分配为代表? Is it your VC assigning itself during initialisation/view did load? 是您的VC在初始化/加载视图期间分配自身吗? If you're not doing that I would assign it in ViewDidLoad. 如果您不这样做,我会在ViewDidLoad中分配它。

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

Also you would probably want to set the property in AppDelegate weak instead of strong to avoid retaining the VC when/if it is dismissed. 另外,您可能希望将AppDelegate中的属性设置为弱而不是强,以避免/如果VC被禁用则保留VC。

Use: 采用:

[[UIApplication sharedApplication].testDelegate = self;

instead of all that: 而不是所有:

self.appDelegate = [[UIApplication sharedApplication] delegate];
self.appDelegate.testDelegate = self;

your function declaration is: 您的函数声明为:

-(void)testFunction:(NSString *)testString;

But you call it as: 但您称其为:

[self.testDelegate testFunction:self];

so you are sending self to a parameter that is expecting a pointer to an NSString , which is obviously incorrect. 因此,您将self发送给一个期望指向NSString指针的参数,这显然是不正确的。

Also, rather than using a timer, I'd use GCD like so: 另外,我不会像使用计时器那样使用GCD:

double delayInSeconds = 10.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));

dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    if [self.testDelegate respondsToSelector:@selector(testFunction:)] {
        [self.testDelegate testFunction:@"Test String"];
    }
});

check like 检查像

if(!testDelegate) {
   [self.testDelegate testFunction:self];
}

and if your controller is in your navigation controller then loop through and find your controller alive object and assign delegate. 如果控制器在导航控制器中,则循环遍历并找到控制器的活动对象并分配委托。

But I would recommended this kinda functionality using LocalNotification 但是我会推荐使用LocalNotification的这种功能

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

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