2014-12-13 13 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

相關問題