浅谈 iOS设计之多视图—模态视图的基本操作

本文详细介绍了如何使用iOS开发和Swift语言创建一个简单的应用程序,包括创建工程、设计基本界面和实现导航逻辑,通过点击视图上的“Back”和“Next”按钮实现返回上一页和进入下一页的功能。
设计的基本思路为:首先先创建一个工程  之后在工程上创建三个类(  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;
}
 
下面好就是在工程上创建的三个类进行操作
 
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 {
    [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];
}
 
//进入下一页的方法时间
-(void)nextPage
{
    SecondViewController *secondVc=[[SecondViewController alloc]init];
    [self presentViewController:secondVc animated:YES completion:^{
   
       NSLog(@"欢迎进入本页面");
    }];
 
}
 
- (void)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.
}
*/ 
@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”按钮就会返回上一页和进入下一页
 

转载于:https://www.cnblogs.com/guiyangxueyuan/p/5276875.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值