2014-04-07 50 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