题目
页面主要内容基于StoryBoard上。
制作四步注册界面:
- 用户名/密码
- email / 全名
- 界面上要提示用户名
- 可返回上一步,应能显示之前填写的资料。
- 如果返回过上一步,再进来,之前填写过的资料要有。
- 确认填写的信息是否正确。
- 显示所有填写过的信息,密码用 * 代替。
- 可返回上一步,应能显示之前填写的资料。
- 创建成功提示。
- 要提到用户名。
- 放一个注册下一个用户,要能直接跳到第1步。
- 如果想加一个主动结束应用的按钮,可调用 exit(0);
StoryBoard创建
- 界面概要
![storyboard][1]
- 细节部分。
点击确定后,segue以show方法连接到下一个界面。
![show-segue][2]
不需要编辑的TextField的Enabled不需要开启。
![disable][3]
3.unwind返回
对应每一个Scene都创建一个ViewController
![目录][4]
在返回的ViewController里面创建对应方法。
在FirstViewController中创建
-(IBAction)unwindSegueToFirstController:(UIStoryboardSegue *)segue{
}
在SecondController中创建
-(IBAction)unwindSegueToSecondController:(UIStoryboardSegue *)segue{
}
这时候回到StoryBoard中,右键点击按钮,其中就有了unwindSegueToFirstController与unwindSegueToSecondController方法,这时候将方法与Button连接。(SecondController中的返回是返回到FirstController,就将unwindSegueToFirstController方法与SecondController中的返回按钮进行连接)
![Exit][5]
第一阶段结束
注意其实有些不能更改的TextField可以用Label去替代,但是考虑到介绍时外观不够直观,所以用了关闭了Enabled的TextField。
封装
StoryBoard布置好以后,首先将控件与对应的ViewController相关联,并且为了安全性,不暴露给其他的对象,所以放在m文件的接口中。
@interface FirstViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nicknameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@end
如果控件都写到m文件中,外部就无法读取,那么怎么传递数据?
我们可以在h文件中创建接受信息的对象。比如需要传递到SecondViewController中的nickname(用户名)。
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@property(nonatomic,copy) NSString *nickname;
@end
但是这时候要仔细想一想,FirstViewContoller到SecondViewController只传递了nickname,那么SecondViewController到ThirdViewController中也是需要password的。反过来当SecondViewController返回时,若FirstViewContoller不存储SecondViewController中的email以及username,那么当FirstViewContoller再一次跳转到SecondViewController时,因为没有存储数据,所以SecondViewController中不能显示之前填写过的资料。
所以在h文件中应该是这样的:
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@property(nonatomic,copy) NSString *nickname;
@property(nonatomic,copy) NSString *email;
@property(nonatomic,copy) NSString *username;
@property(nonatomic,copy) NSString *password;
@end
如果在每个ViewController里面都这么写,会显得很拖沓。这时候我们要把它封装一下。
创建一个BLNMessage类,将属性放在BLNMessage的接口中。
#import <Foundation/Foundation.h>
@interface BLNMessage : NSObject
@property(nonatomic,copy) NSString *nickname;
@property(nonatomic,copy) NSString *email;
@property(nonatomic,copy) NSString *username;
@property(nonatomic,copy) NSString *password;
@end
然后在ViewController创建BLNMessage类的属性。
#import <UIKit/UIKit.h>
#import "BLNMessage.h"
@interface FirstViewController : UIViewController
@property(nonatomic,strong)BLNMessage *message;
@end
封装介绍完毕
传递数据
具体来说,我们主要需要用到这个方法
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
}
该方法在触发Segue跳转的时候会被调用。
以下代码写在FirstViewController中。
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (self.message==nil)
{
self.message=[[BLNMessage alloc]init];
}
//封装
self.message.nickname = self.nicknameTextField.text;
self.message.password=self.passwordTextField.text;
//判断是否跳转至SecondViewController
if([segue.destinationViewController isMemberOfClass:[SecondViewController class]])
{
SecondViewController *sc = (SecondViewController *)segue.destinationViewController;
//传递信息
sc.message=self.message ;
}
}
将数据传递到SecondViewController后,在ViewDidLoad里面取出数据。
- (void)viewDidLoad {
[super viewDidLoad];
self.nickNameTextField.text=self.message.nickname;
self.emailTextField.text=self.message.email;
self.userNameTextField.text=self.message.username;
}
问题一:为什么要添加这句话?
if (self.message==nil)
{
self.message=[[BLNMessage alloc]init];
}
问题二:还有什么地方没有考虑到?或者是有问题的地方?
传递数据内容讲解内容太多,所以不一一详述。
结尾
该习题重点考查三个方面
- unwind用法
- 封装
- -(void)prepareForSegue:(UIStoryboardSegue )segue sender:(id)sender方法的应用(重要*)
希望同学们看完之后一定要好好消化。
[1]: http://static.zybuluo.com/zandhappy/21efbesj2aa6azcg04113203/1.png
[2]: http://static.zybuluo.com/zandhappy/1ybf382oieje0qqqgnbjeird/3.png
[3]: http://static.zybuluo.com/zandhappy/4si2l8wk4m7jbpr7bi1qoc2v/2.png
[4]: http://static.zybuluo.com/zandhappy/s9yuq2x9l6n551fc9g0b98en/4.png
[5]: http://static.zybuluo.com/zandhappy/12nbugx1r0cyh0motrfko9bo/5.png
[6]: http://static.zybuluo.com/zandhappy/uyoiyjo20ey1cy69skgxtrgz/6.png
网友评论