我创建了一个按钮并在运行时添加了它的动作。整个视图在运行时创建。
在点击时调用操作saveUserProfileSetting
会导致应用"NSInvalidArgument: Unrecornized selector sent to a instance
崩溃。当代码存在时,它工作正常
另一堂课。我试图为它创建一个新类,它崩溃了。
@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];
}
调用方法是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];
}
日志:
由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'+ [LASplashViewer saveUserProfileSetting]:无法识别的选择器发送到类0x2a4dc'
请指导我。感谢
答案 0 :(得分:3)
[LASplashViewer saveUserProfileSetting]
崩溃,因为它不是静态函数。将您的功能设为
1)+ (void)saveUserProfileSetting { }
2)或致电
LASplashViewer *viewer = [[LASplashViewer alloc] init];
[viewer saveUserProfileSetting];
答案 1 :(得分:0)
看起来你从类方法
调用实例选择器 LASplashViewer
有类方法showSplashScreen
,您可以在其中调用实例方法saveUserProfileSetting
要解决问题,请尝试制作saveUserProfileSetting
类方法(将-
替换为+
)
答案 2 :(得分:0)
+(void) showSplashScreen;
是一种类方法,您可以从中为按钮
[saveButton addTarget:self action:@selector(saveUserProfileSetting) forControlEvents:UIControlEventTouchUpInside];
此处目标是self
。在类方法中self
引用它自己的Class
而不是实例。所以这里方法的目标是LASplashViewer
而不是它的实例。您的按钮需要类+(void)saveUserProfileSetting
之类的类方法。它没有找到具有此类名称的类方法。它崩溃了。我希望你理解根本原因。
解决方案是将方法作为类方法
+ (void)saveUserProfileSetting