Reputation: 95
I have created a button and added its action at runtime. The entire view is created at runtime.
Calling the action saveUserProfileSetting
on click crashes the app "NSInvalidArgument: Unrecornized selector sent to a instance
. It was working okay when the code was present in
another class. I tried to create an new class for it and it crashes.
@interface LASplashViewer : NSObject
+(void) showSplashScreen;
+(void) dismissSplashScreen;
@end
(void) showSplashScreen
{
UIView *mainScreen = [[[UIApplication sharedApplication]delegate]window];
UIView *windowBlocker = [[UIView alloc]initWithFrame:mainScreen.frame];
windowBlocker.tag = 999;
windowBlocker.backgroundColor = [UIColor clearColor];
UIButton *saveButton = [[UIButton alloc]initWithFrame:CGRectMake(200, 430, 420, 30)];
[saveButton setTitle:@"Save" forState:UIControlStateNormal];
[saveButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[saveButton addTarget:self action:@selector(saveUserProfileSetting) forControlEvents:UIControlEventTouchUpInside];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(165, 200, 450, 480)];
imageView.layer.cornerRadius=10;
imageView.layer.masksToBounds = YES;
[windowBlocker addSubview:imageView];
[imageView addSubview:saveButton];
imageView.userInteractionEnabled = true;
[mainScreen addSubview:windowBlocker];
}
-(void) saveUserProfileSetting
{
// TODO: if validation is successful save the below data. and dismiss the splash screen.
NSUserDefaults *userSettings = [[NSUserDefaults alloc] init];
[userSettings setObject:fullName.text forKey:@"name_pref"];
NSLog(@"%@fullname" , fullName);
[userSettings setObject:jobTitle.text forKey:@"title_pref"];
[userSettings setObject:streetName.text forKey:@"street_name_pref"];
[userSettings setObject:suburbs.text forKey:@"suburb_pref"];
[userSettings setObject:postCode.text forKey:@"postcode_pref"];
[userSettings setObject:phoneNum.text forKey:@"phone_no_pref"];
[userSettings setObject:fax.text forKey:@"fax_pref"];
[userSettings setObject:mobileNumber.text forKey:@"mobile_no_pref"];
[userSettings setObject:email.text forKey:@"email_pref"];
[userSettings synchronize];
}
Call to the method is Show is in another class - // HomeViewController.
(void)viewDidLoad
{
[super viewDidLoad];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isFirstLaunch"])
{
// app already launched
[[NSUserDefaults standardUserDefaults]synchronize];
}
else
{
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"isFirstLaunch"];
[[NSUserDefaults standardUserDefaults]synchronize];
// call this method only for the first load.
//[self performSelector:@selector() withObject:nil afterDelay:0.5];
[self performSelector:@selector(saveProfile) withObject:Nil afterDelay:0.5];
}
}
-(void) saveProfile
{
[LASplashViewer showSplashScreen];
}
log :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[LASplashViewer saveUserProfileSetting]: unrecognized selector sent to class 0x2a4dc'
please guide me. Thanks
Upvotes: 0
Views: 492
Reputation: 42977
+(void) showSplashScreen;
is a class method, from which you are setting action for the button
[saveButton addTarget:self action:@selector(saveUserProfileSetting) forControlEvents:UIControlEventTouchUpInside];
Here the target is self
. Inside a class method self
refers to its own Class
not to the instance. So here the target of the method is LASplashViewer
not its instance. Your button expects a class method like +(void)saveUserProfileSetting
. It is not finding a class method with such name. So its crashing. I hope you understood root cause.
Solution is make method as a class method
+ (void)saveUserProfileSetting
Upvotes: 0
Reputation: 1599
[LASplashViewer saveUserProfileSetting]
is crashing because it is not a static function. Make your function as
1) + (void)saveUserProfileSetting { }
2) Or call as
LASplashViewer *viewer = [[LASplashViewer alloc] init];
[viewer saveUserProfileSetting];
Upvotes: 3
Reputation: 5684
Looks like you call instance selector from class method
LASplashViewer
have class method showSplashScreen
where you call instance method saveUserProfileSetting
to solve problem try make saveUserProfileSetting
also class method (replace -
to +
)
Upvotes: 0