我想在AppDelegate
中调用来自ViewController.m
的函数。我将很好地访问该功能,但View Controller功能无效。
在AppDelegate.m
文件中我有:
#import "AppDelegate.h"
#import "UserList.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSString *UserName = [NSString stringWithFormat:@"%@",[[userInfo objectForKey:@"aps"] objectForKey:@"from"]];
if (application.applicationState == UIApplicationStateActive)
{
UserList *ul = [[UserList alloc] init];
[ul AddUserWhenNotExists:UserName];
}
}
UserList.m
视图控制器文件:
#import "UserList.h"
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)accessFromAppDelegate
{
UIImageView *setting =[[UIImageView alloc] initWithFrame:CGRectMake(0,20,61,60)];
setting.image=[UIImage imageNamed:@"setting_icon.png"];
[setting setUserInteractionEnabled:YES];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SettingClick:)];
[singleTap setNumberOfTapsRequired:1];
[setting addGestureRecognizer:singleTap];
[self.view addSubview:setting];
}
在UserList
视图控制器中不附加setting_icon.png
。我的代码错了吗?请帮忙。
感谢您的时间。
答案 0 :(得分:1)
您可能想要使用NSNotificationCenter:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html
当事情发生时,您可以使用它来告诉您的UIViewController。这是一些示例代码: https://stackoverflow.com/a/9127852/2661880