ryr*_*yan 1 objective-c uiactionsheet uipopovercontroller ios
我目前正在开发一个应用程序,它需要一个使用多种服务"共享"的选项; 比如电子邮件,推特.
为此,我有一个UIBarButtonItem编码输入,当触摸时,它会触发:
UIActionSheet *sheet = [[UIActionSheet alloc]
initWithTitle:@""
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[sheet addButtonWithTitle:@"Email"];
[sheet addButtonWithTitle:@"Tweet"];
[sheet addButtonWithTitle:@"Cancel"];
sheet.cancelButtonIndex = sheet.numberOfButtons-1;
[sheet showFromRect:self.view.bounds inView:self.view animated:YES];
[sheet release];
Run Code Online (Sandbox Code Playgroud)
与此相结合以检测选择了哪个按钮:
clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == actionSheet.cancelButtonIndex) { return; }
switch (buttonIndex) {
case 0:
{
[self emailThis];
break;
}
case 1:
{
[self tweetThis];
break;
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于iPhone.但不幸的是它在iPad上显示不正确.看起来它正在尝试显示UIPopoverController,但它位于导航栏的中心,几乎没有高度.
我已经研究过使用UIPopoverController,但我似乎无法找到如何将它与按钮一起使用.无论如何我可以调整上面的代码来正确显示按钮,因为它已经尝试了.
非常感谢,
瑞安
PS:我是Objective-c/iOS编码的新手,所以请具体说明.谢谢 :)
编辑: 我尝试过使用:
[sheet showFromBarButtonItem:[self share] animated:YES];
Run Code Online (Sandbox Code Playgroud)
但它不起作用.这是我的按钮代码:
UIBarButtonItem *share = [[UIBarButtonItem alloc]
initWithTitle:@"Share"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(loadShare)];
self.navigationItem.rightBarButtonItem = share;
[share release];
Run Code Online (Sandbox Code Playgroud)
这里也是.h文件中的代码:
@interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UIBarButtonItem *share;
}
@property (nonatomic, retain) IBOutlet UIBarButtonItem *share;
Run Code Online (Sandbox Code Playgroud)
这是调试错误:
由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' * - [__ NSPlaceholderArray initWithObjects:count:]:尝试从对象[0]中插入nil对象
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionPhotoShare:)] autorelease];
Run Code Online (Sandbox Code Playgroud)
...
-(void)actionPhotoShare:(id)sender
{
actionSheetShare = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:NSLocalizedString(@"ActionSheet_Cancel", @"")
otherButtonTitles:NSLocalizedString(@"ActionSheet_Email", @""),nil];
if (IS_DEVICE_IPAD) {
[actionSheetShare showFromBarButtonItem:sender animated:YES];
}else {
[actionSheetShare showInView:self.view];
}
}
Run Code Online (Sandbox Code Playgroud)