ChrisC
ChrisC

Reputation: 932

Customizing AppDelegate, adding a ViewController property, then setting this property: unrecognized selector sent to instance 0x16574320

I am not sure if this is the best architecture or not, or if I am going to run into trouble with this, but I am fairly new at objective-c and iOS development, and this is my first attempt at using delegation, etc. in my own architecture.

Here is my AppDelegate interface:

#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

As one can see, there is a UIViewController property.

In another class, I want to access and set this property.

Here is the interface of this class:

#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

and in method SignInButtonPress, I want to do exactly this (set the UIViewController property of the AppDelegate instance handling the app):

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

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

The app crashes on the second line of that method (myAppDel.currentViewController = self;), with the following being output to the console:

2014-12-13 16:55:23.744 myApp1919:60b] -[AppDelegate setCurrentViewController:]: unrecognized selector sent to instance 0x1653c010 2014-12-13 16:55:23.753 myApp[1919:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppDelegate setCurrentViewController:]: unrecognized selector sent to instance 0x1653c010' * First throw call stack: (0x305a6f83 0x3ad57ccf 0x305aa917 0x305a9203 0x304f8768 0x5fe97 0x32df9037 0x32df8fd7 0x32df8fb1 0x32de4717 0x32df8a2f 0x32df8701 0x32df36cb 0x32dc88cd 0x32dc6f77 0x3057220b 0x305716db 0x3056fecf 0x304daebf 0x304daca3 0x353e0663 0x32e2714d 0x7167d 0x3b264ab7) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

So, my question is, why is this not working? Am I simply missing something, like adding protocol compliance to one of this classes, or am I missing something else? Or is there something incredibly wrong with this kind of architecture to begin with?

Thanks!

best regards,

C

Upvotes: 1

Views: 467

Answers (1)

Mrunal
Mrunal

Reputation: 14118

Use self.window.rootViewController = <any viewcontroller reference>;rather than window.currentViewController.

There is no such method named, currentViewController to set.

Upvotes: 3

Related Questions