2014-04-07 44 views
0

我已经创建了一个按钮并在运行时添加了其操作。整个视图在运行时创建。 在点击时调用动作saveUserProfileSetting会导致应用程序崩溃"NSInvalidArgument: Unrecornized selector sent to a instance。当代码出现在 其他课程中时,它工作正常。我试图为它创建一个新类,它崩溃了。按钮上的应用程序崩溃单击操作iOS

@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]; 
    } 

调用方法是显示在另一个 类 - // 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]; 
} 

日志:

终止应用程序由于未捕获的异常 'NSInvalidArgumentException',原因: '+ [LASplashViewer saveUserProfileSetting]:无法识别的选择发送到类0x2a4dc'

请引导我。由于

+0

其中具有u定义saveProfile方法? –

+0

'[self performSelector:@selector(saveProfile)withObject:Nil afterDelay:0.5];'我觉得你的问题在这里 – Zhans

+0

plese看看我最近的编辑。 – LUI

回答

3

[LASplashViewer saveUserProfileSetting]正在崩溃,因为它不是一个静态函数。让你的功能

1)+ (void)saveUserProfileSetting { }

2)或拨打作为

LASplashViewer *viewer = [[LASplashViewer alloc] init]; 
[viewer saveUserProfileSetting]; 
0

看起来你从类方法调用实例选择

LASplashViewer有类方法showSplashScreen在那里你调用实例方法saveUserProfileSetting

需要解决的问题尽量让saveUserProfileSetting也是类方法(更换-+

0

+(void) showSplashScreen;是一个类的方法,从中你的按钮

[saveButton addTarget:self action:@selector(saveUserProfileSetting) forControlEvents:UIControlEventTouchUpInside]; 

这里设置动作目标是self。内部类方法self指的是它自己的Class而不是实例。所以这里的方法的目标是LASplashViewer而不是它的实例。你的按钮需要类似+(void)saveUserProfileSetting这样的方法。它没有找到具有这种名称的类方法。所以它崩溃了。我希望你了解根本原因。

解决方案是使方法作为类方法

+ (void)saveUserProfileSetting