使用此代码打开操作表。 在iPhone上工作得很好,但在iPad上它会崩溃。我认为这是因为arm64。可能我必须将Actionsheet转换为popover。
如何更改此代码?
- (void)pushedNewBtn
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Photo Library", nil];
[sheet showInView:self.view.window];
}
答案 0 :(得分:1)
为了正确使用UIAlertView
,您需要使用委托方法来响应按下的按钮。
在您的头文件中,您需要以下
@interface ViewController : UIViewController <UIAlertViewDelegate>
您已使用以下行UIAlertView
正确设置了delegate:self
变量的代理。
现在,按下UIAlertView
中的按钮后,代理人将调用以下方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
然后,您可以使用与此
类似的代码响应按钮switch (buttonIndex) {
case 0:
//Cancel button code goes here if needed
break;
case 1:
//First 'other' button code goes here
break;
case 2:
//second 'other' button code goes here
break;
default:
break;
}
我希望这会有所帮助
答案 1 :(得分:1)
这是工作结果:
- (void)pushedNewBtn {
UIAlertView *sheet = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Camera", @"Photo Library", nil];
[sheet show];
}
- (void)libraryButton {
UIImagePickerController *pickerLibrary = [[UIImagePickerController alloc] init];
pickerLibrary.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
pickerLibrary.delegate = self;
[self presentModalViewController:pickerLibrary animated:YES];
}
- (void)cameraButton {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
#pragma mark- alertView delegate PICKER
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
return;
break;
case 1:
[self cameraButton];
break;
case 2:
[self libraryButton];
break;
default:
break;
}
}
但现在我有了新问题, 我想插入新的alertviev但不起作用
- (void)pushedSaveBtn {
UIAlertView *sheet2 = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Save", nil];
[sheet2 show];
}
- (void)savemeButton {
//codice da inserire
}
#pragma mark- alertView2 delegate SAVER
- (void)alertView2:(UIAlertView *)alertView2 clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
return;
break;
case 1:
[self savemeButton];
break;
default:
break;
}
}