2011-11-12 63 views
1

我发现了几个帖子在网上指出,我可以从任何视图控制器通过下面的调用访问我的应用程序委托对象:读委托

[[UIApplication sharedApplicaton] delegate]; 

(例如:data between Views UIApplicationiOS - Calling App Delegate method from ViewController

但是,每当我将这一行包含在我的一个视图控制器的某个函数中时,应用程序就会崩溃。

这是我正在编写的第一个应用程序,我看不到我的代码和其他帖子如何说我应该使用此sharedApplication调用之间的区别。为了完整起见,下面是我的应用程序委托和视图控制器的摘录。

FirstViewController.h:

@class wStreamAppDelegate; 
#define URL_ADDRESS @"http://google.com" 

@interface FirstViewController : UIViewController <UIWebViewDelegate>{ 
    IBOutlet UIWebView * webView; 
wStreamAppDelegate* appDelegate; 
} 

@property(nonatomic,retain) wStreamAppDelegate* appDelegate; 
@property(nonatomic,retain) IBOutlet UIWebView* webView; 

FirstViewController.m:

#import "FirstViewController.h" 
#import "wStreamAppDelegate.h" 

@implementation FirstViewController 
@synthesize webView,appDelegate; 
@class wStreamAppDelegate; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
NSString* urlAddress = URL_ADDRESS; 
NSURL* url = [NSURL URLWithString:urlAddress]; 
NSURLRequest * requestObj = [NSURLRequest requestWithURL:url]; 
[webView loadRequest:requestObj]; 

    self.appDelegate = (wStreamAppDelegate*)[[UIApplication sharedApplicaton] delegate]; 

    //This doesn't work either 
    // wStreamAppDelegate *appDelegate= (wStreamAppDelegate*)[[UIApplication sharedApplicaton] delegate]; 

wStreamAppDelegate.h:

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

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

@end 

如果任何人有什么可能会错误的任何想法,一般调试这些问题的建议或技巧,我真的很感激它。谢谢。

+0

要开始,请转到运行>>控制台打开控制台,查看导致问题的具体错误。 –

+0

感谢回复如此迅速: [会话开始于2011-11-11 22:38:37 -0800。] 2011-11-11 22:38:39.032 wStream [647:207] + [UIApplication sharedApplicaton]:无法识别的选择器发送到类0x1ea2898 2011-11-11 22:38:39.036 wStream [647:207] ***由于未捕获的异常'NSInvalidArgumentException',原因:'+ [UIApplication sharedApplicaton]:无法识别的选择器发送到类0x1ea2898' ***一次调用堆栈: –

+0

试试这个... appDelegate = [[UIApplication sharedApplicaton] delegate]; – Sahil

回答

0

错别字每个人迟早会得到......在这种情况下,您写了“sharedApplicaton”而不是“sharedApplication”。

+0

就是这样。感谢您的回应。我现在接受你的回答。 –