【问题标题】:Read delegate from view controller in tab bar application从标签栏应用程序中的视图控制器读取委托
【发布时间】:2011-12-27 12:38:19
【问题描述】:

我在网上找到了几篇帖子,说明我可以通过以下调用从任何视图控制器访问我的应用程序委托对象:

[[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

如果有人对可能出现的问题有任何想法、调试此类问题的一般建议或提示,我将不胜感激。谢谢。

【问题讨论】:

  • 要开始,请转到运行 >> 控制台以打开控制台并查看导致您的问题的具体错误。
  • 感谢您如此迅速地回复:[会话开始于 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' *** 第一次抛出调用堆栈:
  • 试试这个... appDelegate = [[UIApplication sharedApplicaton] delegate];

标签: iphone uitabbarcontroller uiapplicationdelegate


【解决方案1】:

错别字迟早会引起所有人的注意……在这种情况下,您写的是“sharedApplicaton”而不是“sharedApplication”。

【讨论】:

  • 就是这样。感谢您的回复。我现在接受你的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-23
  • 1970-01-01
  • 1970-01-01
  • 2015-08-22
  • 2011-12-29
  • 2012-08-09
相关资源
最近更新 更多