浅谈 iOS设计之多视图—模态视图的基本操作
设计的基本思路为:首先先创建一个工程 之后在工程上创建三个类( FirstViewController\ SecondViewController\ ThirdViewController)
首先在 AppDelegate中创建根是视图 代码如下
AppDelegate.h
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
FirstViewController *firstVc=[[FirstViewController alloc]init];
//创建根视图
self.window.rootViewController=firstVc;
self.window.backgroundColor=[UIColor grayColor];
return YES;
self.window.backgroundColor=[UIColor grayColor];
return YES;
}
下面好就是在工程上创建的三个类进行操作
FirstViewController.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface FirstViewController : UIViewController
//声明按钮属性
@property(strong,nonatomic)UIButton *Button;
@end
对第一个类的操作
FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置主视图的背景色
self.view.backgroundColor=[UIColor greenColor];
//创建按钮
self.Button=[[UIButton alloc]initWithFrame:CGRectMake(100, 200, 50, 50)];
self.Button.backgroundColor=[UIColor redColor];
[self.Button setTitle:@"Next" forState:UIControlStateNormal];
//调用方法实现进入下一页面
[self.Button addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.Button];
self.Button.backgroundColor=[UIColor redColor];
[self.Button setTitle:@"Next" forState:UIControlStateNormal];
//调用方法实现进入下一页面
[self.Button addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.Button];
}
//进入下一页的方法时间
-(void)nextPage
{
SecondViewController *secondVc=[[SecondViewController alloc]init];
[self presentViewController:secondVc animated:YES completion:^{
NSLog(@"欢迎进入本页面");
}];
}
-(void)nextPage
{
SecondViewController *secondVc=[[SecondViewController alloc]init];
[self presentViewController:secondVc animated:YES completion:^{
NSLog(@"欢迎进入本页面");
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
[super didReceiveMemoryWarning];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
对第二个类的操作
SecondViewController.h
#import <UIKit/UIKit.h>
#import "ThirdViewController.h"
@interface SecondViewController : UIViewController
@property(strong,nonatomic)UIButton *Button;
@end
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor yellowColor];
//返回按钮
self.Button=[[UIButton alloc]initWithFrame:CGRectMake(100, 200, 50, 50)];
[self.Button setTitle:@"Back" forState:UIControlStateNormal];
[self.Button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//调用方法实现返回上一页
[self.Button addTarget:self action:@selector(textFrontPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.Button];
//进入下一页的按钮
self.Button=[[UIButton alloc]initWithFrame:CGRectMake(200, 200, 50, 50)];
[self.Button setTitle:@"Next" forState:UIControlStateNormal];
[self.Button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//调用方法实现进入下一页
[self.Button addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.Button];
}
//返回上一页的方法
-(void)textFrontPage
{
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"front page show");
}];
}
-(void)nextPage
{
ThirdViewController *thirdVC=[[ThirdViewController alloc]init];
[self presentViewController:thirdVC animated:YES completion:^{
NSLog(@"欢迎进入本页");
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
对第三个类的操作
ThirdViewController.h
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface ThirdViewController : UIViewController
@property(strong,nonatomic)UIButton *Button;
@end
ThirdViewController.m
#import "ThirdViewController.h"
@interface ThirdViewController ()
@end
@implementation ThirdViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor blueColor];
//返回按钮
self.Button=[[UIButton alloc]initWithFrame:CGRectMake(100, 200, 50, 50)];
[self.Button setTitle:@"Back" forState:UIControlStateNormal];
[self.Button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//调用方法实现返回上一页
[self.Button addTarget:self action:@selector(textFrontPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.Button];
//进入首页按钮
self.Button=[[UIButton alloc]initWithFrame:CGRectMake(200, 200, 50, 50)];
[self.Button setTitle:@"Next" forState:UIControlStateNormal]; ;
[self.Button setTitleColor:[UIColor redColor ] forState:UIControlStateNormal];
//调用方法实现进入首页
[self.Button addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.Button];
}
//返回方法
-(void)textFrontPage
{
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"Front page show");
}];
}
//进入下一页的方法
-(void)nextPage
{
FirstViewController *firstVc=[[FirstViewController alloc]init];
[self presentViewController:firstVc animated:YES completion:^{
NSLog(@"欢迎进入本页");
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
结果效果图
点击视图上的“Back”和“Next”按钮就会返回上一页和进入下一页