data between Views UIApplication

落花浮王杯 提交于 2020-01-06 12:44:17

问题


i want to share data between views...

i have the appdelegate of tabbar application:

myappdelegate.h

#import <UIKit/UIKit.h>

@interface myappdelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
    UIWindow *window;
    UITabBarController *tabBarController;
    NSString  *result;
}


@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@property (copy , readwrite) NSString *result;


@end

if i want to call with this command, there is the hint: "may not respond"....

   myappdelegate *dataCenter = [(myappdelegate *)[UIApplication sharedApplication] delegate];  <<may not respond
   dataCenter.result = @"msg";

result_view *resultView = [[result_view alloc] initWithNibName:@"result_view" bundle:nil];
[self.navigationController pushViewController:resultView animated:YES];
[resultView release];

result_view.m

- (void)viewDidLoad
{
    myappdelegate *dataCenter = (myappdelegate*)[[UIApplication sharedApplication]delegate];

    [label setText:dataCenter.result];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

the program crashes...


回答1:


Your code is saying that the sharedApplication is of the class myappdelegate, which indeed does not respond to delegate. Do this:

(myappdelegate *)[[UIApplication sharedApplication] delegate];

to remove the warning.


Due to Objective-C's runtime messaging, your current (warning-generating) code won't crash the app. The crash lies somewhere else.




回答2:


Your first line should be

myappdelegate *dataCenter = (myappdelegate *)[[UIApplication sharedApplication] delegate];

As for the second line, I can't tell what you expect to happen. You don't have a result_array property on your myappdelegate class, so of course you can't set that property.

If you were trying to set the result property, you should have written

dataCenter.result = @"msg";


来源:https://stackoverflow.com/questions/5407678/data-between-views-uiapplication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!