UIAlertView 和 UIActionSheet简单应用

来源:互联网 发布:广联达软件难学吗 编辑:程序博客网 时间:2024/05/02 09:42


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.


    [selfUIActionSheetButton];

    [selfAlertViewButton];

  

}


#pragma mark - UIActionSheet 


- (void)UIActionSheetButton

{

    /* 创建一个 button  */

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];

    button.frame = CGRectMake(40, 60, 300, 40);

    button.backgroundColor = [UIColorcyanColor];

    [button setTitle:@"UIActionSheet效果"forState:UIControlStateNormal];

    [self.viewaddSubview:button];

    

    /* 添加点击事件 */

    [button addTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];


}

- (void)buttonClicked:(UIButton *)button

{


    /* UIActionSheet

     * initWithTitle: 设置标题, 将会显示在 ActionSheet 的顶部

     * delegate: 设置 ActionSheet的一个按钮被按下后, 它的 delegate 将会被通知, 并且这个 delegate actionSheet : didDismissWithButtonIndex方法将会执行.

     * cancelButtonTitle: 设置取消按钮的标题,这个按钮将会显示在最下面

     * destructiveButtonTitle: 设置第一个确定按钮的标题,这个按钮可以理解成"好的,继续"

     * otherButtonTitles: 可以设置任意多的确定按钮

     */

    UIActionSheet *action = [[UIActionSheetalloc] initWithTitle:@"你喜欢我吗?"delegate:selfcancelButtonTitle:@"不是不是的"destructiveButtonTitle:@"是的,我喜欢你啊!"otherButtonTitles:@"",nil];

    [action showInView:self.view];

}


#pragma mark - AlertView


- (void)AlertViewButton

{

    /* 创建 button 对象 */

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];

    button.frame = CGRectMake(40, 120, 300, 40);

    button.backgroundColor = [UIColorcyanColor];

    [button setTitle:@"UIAlertView效果"forState:UIControlStateNormal];

    [self.viewaddSubview:button];

    

    /* 添加点击事件 */

    [button addTarget:selfaction:@selector(button1Clicked:)forControlEvents:UIControlEventTouchUpInside];


}


- (void)button1Clicked:(UIButton *)button1Clicked

{


    /* UIAlertView

     * initWithTitle: 设置标题, 将会显示在 Alert 的顶部

     * message: 设置提示消息内容

     * delegate: 设置 Alert 的委托, 这里设为 self

     * cancelButtonTitle: 设置取消按钮的标题

     * otherButtonTitles: 设置多个按钮

     */

    UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"完美告白"message:@"我喜欢你呀"delegate:selfcancelButtonTitle:@"好的"otherButtonTitles: nil];

    [alert show];


}


# pragma mark - UIAlertView UIActionSheet的不同


/* Alert 相当于 Windows 中的 Messagebox , Action Sheet也是类似. 不同的是, Alert可以只有一个选择项, Action Sheet 却至少要两个选项  */
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

[self UIActionSheetButton];
[self AlertViewButton];

}

#pragma mark - UIActionSheet

- (void)UIActionSheetButton
{
/* 创建一个 button */
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(40, 60, 300, 40);
button.backgroundColor = [UIColor cyanColor];
[button setTitle:@"UIActionSheet 效果" forState:UIControlStateNormal];
[self.view addSubview:button];

/* 添加点击事件 */
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

}
- (void)buttonClicked:(UIButton *)button
{

/* UIActionSheet
* initWithTitle: 设置标题, 将会显示在 ActionSheet 的顶部
* delegate: 设置 ActionSheet 的一个按钮被按下后, 它的 delegate 将会被通知, 并且这个 delegate 的 actionSheet : didDismissWithButtonIndex 方法将会执行.
* cancelButtonTitle: 设置取消按钮的标题, 这个按钮将会显示在最下面
* destructiveButtonTitle: 设置第一个确定按钮的标题, 这个按钮可以理解成"好的, 继续"
* otherButtonTitles: 可以设置任意多的确定按钮
*/
UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"你喜欢我吗?" delegate:self cancelButtonTitle:@"不是不是的" destructiveButtonTitle:@"是的, 我喜欢你啊!" otherButtonTitles:@"", nil];
[action showInView:self.view];
}

#pragma mark - AlertView

- (void)AlertViewButton
{
/* 创建 button 对象 */
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(40, 120, 300, 40);
button.backgroundColor = [UIColor cyanColor];
[button setTitle:@"UIAlertView 效果" forState:UIControlStateNormal];
[self.view addSubview:button];

/* 添加点击事件 */
[button addTarget:self action:@selector(button1Clicked:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)button1Clicked:(UIButton *)button1Clicked
{

/* UIAlertView
* initWithTitle: 设置标题, 将会显示在 Alert 的顶部
* message: 设置提示消息内容
* delegate: 设置 Alert 的委托, 这里设为 self
* cancelButtonTitle: 设置取消按钮的标题
* otherButtonTitles: 设置多个按钮
*/
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"完美告白" message:@"我喜欢你呀" delegate:self cancelButtonTitle:@"好的" otherButtonTitles: nil];
[alert show];

}

# pragma mark - UIAlertView 和 UIActionSheet 的不同

/* Alert 相当于 Windows 中的 Messagebox , 跟 Action Sheet 也是类似. 不同的是, Alert可以只有一个选择项, 而 Action Sheet 却至少要两个选项 */

0 0
原创粉丝点击