Xcode更新从AppDelegate查看控制器UIImage

时间:2014-07-03 13:49:00

标签: ios objective-c uiviewcontroller

我想在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。我的代码错了吗?请帮忙。

感谢您的时间。

1 个答案:

答案 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