2014-12-13 14 views
1

我不确定这是否是最好的架构,或者如果我要遇到麻烦,但我是在Objective-C和iOS开发中相当新颖,这是我第一次尝试在自己的架构中使用委托等。定制AppDelegate,添加一个ViewController属性,然后设置这个属性:无法识别的选择器发送到实例0x16574320

这里是我的AppDelegate接口:

#import <UIKit/UIKit.h> 
#import <CoreData/CoreData.h> 
#import "XMPPStream.h" 
#import "XMPPController.h" 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 

@property (strong, nonatomic) UIWindow *window; 

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; 
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; 
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; 
@property (strong, nonatomic) UIViewController *currentViewController; 

//@property (strong, nonatomic) XMPPStream *xmppStream; 
@property (strong, nonatomic) XMPPController *myXMPPController; 

- (void)saveContext; 
- (NSURL *)applicationDocumentsDirectory; 


@end 

人们可以看到,有一个UIViewController财产。

在另一个类中,我想访问并设置此属性。

下面是这个类的接口:

#import <UIKit/UIKit.h> 
#import "XMPPStream.h" 

@interface SignInViewController : UIViewController <XMPPStreamDelegate> 
@property (strong, nonatomic) IBOutlet UITextField *UserNameTextField; 
@property (strong, nonatomic) IBOutlet UITextField *PassWordTextField; 
//@property (strong, nonatomic) XMPPStream *myStream; 
- (IBAction)SignInButtonPress:(UIButton *)sender; 

@end 

和方法SignInButtonPress,我想要做的正是这一点(设置AppDelegate的情况下处理应用程序的UIViewController的属性):

- (IBAction)SignInButtonPress:(UIButton *)sender { 

    AppDelegate *myAppDel = (AppDelegate *) [[UIApplication sharedApplication]delegate]; 
    myAppDel.currentViewController = self; 

该应用程序在该方法的第二行崩溃(myAppDel.currentViewController = self;),输出到控制台:

2014-12-13 16:55:23.744 myApp1919:60b] - [AppDelegate setCurrentViewController:]:无法识别的选择器发送到实例 0x1653c010 2014-12-13 16:55:23.753 myApp [1919:60b] *终止应用 由于未捕获的异常 'NSInvalidArgumentException',原因是: ' - [AppDelegate中setCurrentViewController:]:无法识别的选择发送 到实例0x1653c010' *第一掷调用堆栈:(0x305a6f83 0x3ad57ccf 0x305aa917 0x305a9203 0x304f8768 0x5fe97 0x32df9037 0x32df8fd7 0x32df8fb1 0x32de4717 0x32df8a2f 0x32df8701 0x32df36cb 0x32dc88cd 0x32dc6f77 0x3057220b 0x305716db 0x3056fecf 0x304daebf 0x304daca3 0x353e0663 0x3 2e2714d 0x7167d 0x3b264ab7)的libC++ abi.dylib:与类型NSException(LLDB的 未捕获的异常)

所以,我的问题是,这是为什么不工作终止?我是否简单地错过了某些东西,比如向这些类中的一个添加协议遵从性,还是缺少其他东西?或者,这种架构开始时有什么不可思议的错误?

谢谢!

最好的问候,

Ç

回答

3

使用self.window.rootViewController = <any viewcontroller reference>;而非window.currentViewController。

有没有这样的方法命名,currentViewController来设置。

+0

myAppDel.rootViewController没有解决,似乎没有这样的属性。此外,是否将currentViewController作为属性添加到我的appDelegate接口,而不是为此属性创建setter和getter?至少这是我似乎记得的,但也许它不是真的或它应该工作。 – user2011985 2014-12-13 16:22:09

+0

原来我忘记了alloc和init的appDelegate UIViewController属性。但既然你的答案似乎也行得通,我会接受它作为这个问题的正确答案。谢谢! :) – user2011985 2014-12-13 16:28:53

+0

是的,刚刚完成;) – user2011985 2014-12-13 16:42:29

相关问题