Question

i have a loginviewcontroller class that has a button 'login'.

The login class is connected to my mainmenuviewcontroller to a uiview. This i connected as follows

(mainmenuviewcontroller.h)

import <UIKit/UIKit.h> 
@interface ViewController : UIViewController

@property (strong) IBOutlet UIView *loginView;

@end

(mainmenuviewcontroller.m)

- (void)viewDidLoad
{
    [super viewDidLoad];

    LogInViewController *logIn = [[LogInViewController alloc] initWithNibName:@"LogInViewController" bundle:nil];
    [self.loginView addSubview:logIn.view];
    [self.loginView setClipsToBounds:YES];
}

At this point, everything is fine. My loginViewcontroller is showing up niceley inside its own view in my main menu.

When pressing the login button on the loginviewcontroller(showing in a uiview on the main menu), it then calls the method below located in its loginviewcontroller class.

(loginviewcontroller.h)

#import <UIKit/UIKit.h>

@interface LogInViewController : UIViewController
- (IBAction)loginButtonPressed;

@end

(loginviewcontroller.m)

- (IBAction)loginButtonPressed
{
    NSLog(@"login Button Pressed");
}

However an exception occurs and i dont know why: i have ensured the touchup inside sent action from the button has the correct name. And i am using Arc. HELP! :)

2013-10-28 13:57:46.367 Headache Mbl[1593:60b] -[NSConcreteMapTable loginButtonPressed]: unrecognized selector sent to instance 0x17d4fa20
2013-10-28 13:57:46.370 Headache Mbl[1593:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMapTable loginButtonPressed]: unrecognized selector sent to instance 0x17d4fa20'
*** First throw call stack:
(0x2de2fe8b 0x385826c7 0x2de337b7 0x2de320b7 0x2dd80e98 0x305ea55f 0x305ea4fb 0x305ea4cb 0x305d60f3 0x305e9f13 0x305e9bdd 0x305e4c09 0x305b9f59 0x305b8747 0x2ddfaf27 0x2ddfa3ef 0x2ddf8bdf 0x2dd63541 0x2dd63323 0x32a742eb 0x3061a1e5 0x60bdd 0x38a7bab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

I am still a quite new to this, a possible solution was suggested that the class needs to be a property. How is this done? Thanks

Was it helpful?

Solution

If you have connected the IBoutlet properly,

Make the LogInViewController as a ivar or property. The object loses its value on viewDidLoad's return.

@interface ViewController : UIViewController
  {
    LogInViewController *logIn;
  }
    @property (strong) IBOutlet UIView *loginView;
@end

- (void)viewDidLoad
{
    [super viewDidLoad];

    logIn = [[LogInViewController alloc] initWithNibName:@"LogInViewController" bundle:nil];
    [self.loginView addSubview:logIn.view];
    [self.loginView setClipsToBounds:YES];
}

OTHER TIPS

when the viewDidLoad ends the logIn object will be released, only his view will survive because you are adding it to the main view controller view, so when you tap the button, will send a message to an object that doesn't exist anymore.

Let me say that it's conceptually wrong what you are trying to do, the login view controller should be added to a hierarchy, presenting it, pushing it, or containing in another view controller, but not creating it just to get his view and add it to another viewcontroller.

LogInViewController *logIn = [[LogInViewController alloc] initWithNibName:@"LogInViewController" bundle:nil];
[self addChildViewController:logIn];
[self.loginView addSubview:logIn.view];
[self.loginView setClipsToBounds:YES];

turns out i needed to add the view as a a child view controller. Once loaded it removes it from memory but still shows it. so its needs to be added as a child.

:)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top