按下 UIAlerView 按钮后方法没有响应
我遇到这个问题,我显示警报视图,然后按下按钮后某些方法应该运行,但两者都不会。
- (void)insertNewObject //Works fine
{
//AlertView is a subclass of UIAlertView I created.
AlertView *al = [AlertView alloc];
al = [al initWithTitle:@"title" message:@"message" delegate:[self view] cancelButtonTitle:@"cencel" okButtonTitle:@"ok"];
[al.titlebox becomeFirstResponder];
//[view setAlertViewStyle:UIAlertViewStylePlainTextInput];
[al show];
} /Till here everything works fine.
下面的所有代码都不起作用。或者它们起作用了,我只是不知道,因为它们不运行。
//This method does no run al all.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex2
{
if(buttonIndex2 == 0)
{
_buttonIndex = buttonIndex2;
[self showAbortAlert];
} else
{
_buttonIndex = buttonIndex2;
[self addObject];
}
}
- (void)addObject
{
if(_buttonIndex == 1) {
// Create a new instance of the entity managed by the fetched results controller.
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
// If appropriate, configure the new managed object.
// Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
[newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];
//[newManagedObject setValue:<#(id)#> forKey:<#(NSString *)#>];
//[newManagedObject setValue:<#(id)#> forKey:<#(NSString *)#>];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
} else {
[self showAbortAlert];
}
}
- (void)showAbortAlert
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Action Aborted" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert setAlertViewStyle:UIAlertViewStyleDefault];
[alert show];
}
没有编译错误。 提前致谢!
I have this problem where I show an alert view and then after a button is pressed certain methods should run but neither would.
- (void)insertNewObject //Works fine
{
//AlertView is a subclass of UIAlertView I created.
AlertView *al = [AlertView alloc];
al = [al initWithTitle:@"title" message:@"message" delegate:[self view] cancelButtonTitle:@"cencel" okButtonTitle:@"ok"];
[al.titlebox becomeFirstResponder];
//[view setAlertViewStyle:UIAlertViewStylePlainTextInput];
[al show];
} /Till here everything works fine.
All the code below here dont work. Or they work, I just dont know coz they dont run.
//This method does no run al all.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex2
{
if(buttonIndex2 == 0)
{
_buttonIndex = buttonIndex2;
[self showAbortAlert];
} else
{
_buttonIndex = buttonIndex2;
[self addObject];
}
}
- (void)addObject
{
if(_buttonIndex == 1) {
// Create a new instance of the entity managed by the fetched results controller.
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
// If appropriate, configure the new managed object.
// Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
[newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];
//[newManagedObject setValue:<#(id)#> forKey:<#(NSString *)#>];
//[newManagedObject setValue:<#(id)#> forKey:<#(NSString *)#>];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
} else {
[self showAbortAlert];
}
}
- (void)showAbortAlert
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Action Aborted" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert setAlertViewStyle:UIAlertViewStyleDefault];
[alert show];
}
No compilation errors.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道这是否可以解决您所有的问题,但是在您给出的代码示例中,当您希望它成为 ViewController 时,您可能会错误地将委托设置为视图:
也就是说,而不是:
您可能想要这样做:
另外,由于您分配了警报,所以不要忘记在调用 show 后释放它。
I don't know if this fixes all your problems, but in the code example you gave, you are probably setting the delegate falsely to the view, when you want it to be the ViewController:
That is, instead of:
you probably want to do this:
Also, since you alloc'ed the alert, don't forget to release it after you've called show.