iOS-UIActionSheet(已弃用)

2018-03-21  本文已影响567人  我是谁重要吗

已由 UIAlertController 代替

已由 UIAlertController 代替

已由 UIAlertController 代替

UIActionSheet基本写法

 .h 写代理 <UIActionSheetDelegate>

 .m 写实现
UIActionSheet *actiongSheet = [[UIActionSheet alloc] initWithTitle:@"算是标题吧" 
  delegate:self  
  cancelButtonTitle:@"取消按钮吧,最下面吧" 
  destructiveButtonTitle:@"红色按钮吧" 
  otherButtonTitles:@"条目1", @"条目2", @"条目3", nil]; 
 [sheet showFromRect:view.bounds inView:view animated:YES]; 
 [sheet release]; 
}

actiongSheet.actionSheetStyle = UIActionSheetStyleDefault;///有几种,cmd + 左键
actiongSheet.cancelButtonIndex = actiongSheet.numberOfButtons-1;
[actiongSheet showInView:self.view];

问题来了,实际项目中,我们需要的是动态的编辑 otherButtonTitles,肿么办?
先设置为nil ,在添加
UIActionSheet *actiongSheet = [[UIActionSheet alloc] initWithTitle:@"请选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
///////循环添加
for (int i = 0; i<yourArray.count; i++) 
{
       [actiongSheet addButtonWithTitle:yourArray[i]];
}
/////添加cancel 按钮
[actiongSheet addButtonWithTitle:@"取消"];
//////设置刚添加的 取消 按钮为系统默认的 cancel 按钮
actiongSheet.cancelButtonIndex = actiongSheet.numberOfButtons-1;

actiongSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actiongSheet showInView:self.view];

///////点击触发方法
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    nslog(@"do what you want");
}

//有时候点击最后一项按钮的下半部分失效 
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
或者
[sheet showInView:[AppDelegate sharedDelegate].tabBarController.view];
上一篇下一篇

猜你喜欢

热点阅读