iOS9提示框的正确使用方式

2025-05-29 0 100

在从ios8到ios9的升级过程中,弹出提示框的方式有了很大的改变,在xcode7 ,ios9.0的sdk中,苹果已经明确提示不再推荐使用uialertview,而推荐使用uialertcontroller,现在,我们通过代码来演示一下。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46
#import "loginviewcontroller.h"

@interface loginviewcontroller ()

@property (weak, nonatomic) iboutlet uitextfield *password;

@property (weak, nonatomic) iboutlet uitextfield *username;

@property (weak, nonatomic) iboutlet uibutton *login;

- (ibaction)loginonclick:(uibutton *)sender;

@end

@implementation loginviewcontroller

- (void)viewdidload {

[super viewdidload];

//获取通知中心

nsnotificationcenter *center = [nsnotificationcenter defaultcenter];

//注册通知

[center addobserver:self selector:@selector(textchange) name:uitextfieldtextdidchangenotification object:self.username];

[center addobserver:self selector:@selector(textchange) name:uitextfieldtextdidchangenotification object:self.password];

}

-(void)textchange

{

//当用户名框和密码框同时有内容时,登录按钮才可以点击

self.login.enabled = (self.username.text.length > 0 && self.password.text.length > 0);

}

//点击登录按钮执行的事件

- (ibaction)loginonclick:(uibutton *)sender {

if ([self.username.text isequal: @"xiaojin"] && [self.password.text isequal: @"123456"]) {

nslog(@"successful");

[self performseguewithidentifier:@"loginidentifier" sender:nil];

} else {

//ios9以前经常用来创建提示框的方法

uialertview *alert = [[uialertview alloc] initwithtitle:@"提示" message:@"用户名或密码出现错误" delegate:self cancelbuttontitle:@"确定" otherbuttontitles:nil, nil];

[alert show];

}

}

@end

编写上述代码时,会有下列的警告提示:

iOS9提示框的正确使用方式

iOS9提示框的正确使用方式

说明uialertview首先在ios9中被弃用(不推荐)使用。让我们去用uialertcontroller。但是运行程序,发现代码还是可以成功运行,不会出现crash。当输入用户名或密码错误时就会淡出提示框,如图:

iOS9提示框的正确使用方式

iOS9提示框的正确使用方式

但是在实际的工程开发中,我们有这样一个“潜规则”:要把每一个警告(warning)当做错误(error)。所以为了顺应苹果的潮流,我们来解决这个warning,使用uialertcontroller来解决这个问题。代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54
#import "loginviewcontroller.h"

@interface loginviewcontroller ()

@property (weak, nonatomic) iboutlet uitextfield *password;

@property (weak, nonatomic) iboutlet uitextfield *username;

@property (weak, nonatomic) iboutlet uibutton *login;

- (ibaction)loginonclick:(uibutton *)sender;

@end

@implementation loginviewcontroller

- (void)viewdidload {

[super viewdidload];

//获取通知中心

nsnotificationcenter *center = [nsnotificationcenter defaultcenter];

//注册通知

[center addobserver:self selector:@selector(textchange) name:uitextfieldtextdidchangenotification object:self.username];

[center addobserver:self selector:@selector(textchange) name:uitextfieldtextdidchangenotification object:self.password];

}

-(void)textchange

{

//当用户名框和密码框同时有内容时,登录按钮才可以点击

self.login.enabled = (self.username.text.length > 0 && self.password.text.length > 0);

}

//点击登录按钮执行的事件

- (ibaction)loginonclick:(uibutton *)sender {

if ([self.username.text isequal: @"xiaojin"] && [self.password.text isequal: @"123456"]) {

nslog(@"successful");

[self performseguewithidentifier:@"loginidentifier" sender:nil];

} else {

//初始化提示框;

uialertcontroller *alert = [uialertcontroller alertcontrollerwithtitle:@"提示" message:@"用户名或密码出现错误" preferredstyle: uialertcontrollerstylealert];

[alert addaction:[uialertaction actionwithtitle:@"确定" style:uialertactionstyledefault handler:^(uialertaction * _nonnull action) {

//点击按钮的响应事件;

}]];

//弹出提示框;

[self presentviewcontroller:alert animated:true completion:nil];

}

}

@end

看,这样就不会有警告了吧!编译运行后的界面和上面的一样。其中preferredstyle这个参数还有另一个选择:uialertcontrollerstyleactionsheet。选择这个枚举类型后,实现效果如下:

iOS9提示框的正确使用方式

iOS9提示框的正确使用方式

iOS9提示框的正确使用方式

可以发现这个提示框是从底部弹出的。是不是很简单呢?通过查看代码还可以发现,在提示框中的按钮响应不再需要delegate委托来实现了。直接使用addaction就可以在一个block中实现按钮点击,非常方便。
总结,可以发现这里我们呈现一个对话框使用了presentviewcontroller这个方法,这个方法是呈现模态视图(modal view)的方法,也就是是说,此时的提示框是一个模态视图。当我们在进行界面跳转的时候,也一般使用这个方法,此时呈现的第二个viewcontroller也是一个模态视图。我们可以把模态视图理解为一个浮动在原先视图上的一个临时性的视图或者界面,当在模态视图中调用dismissviewcontroller方法时,会返回上一个界面,并销毁这个模态视图对象。

以上就是本文的全部内容,希望能给大家一个参考,也希望大家多多支持快网idc。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 iOS9提示框的正确使用方式 https://www.kuaiidc.com/93949.html

相关文章

发表评论
暂无评论