多視圖控制器編寫

來源:互聯網
上載者:User

標籤:style   blog   http   color   檔案   2014   


#import "AppDelegate.h"#import "FisrtViewController.h"@interface AppDelegate () @end@implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; //聲明一個視圖控制器用於導航器載入 FisrtViewController *fViewController=[[FisrtViewController alloc]init]; fViewController.view.backgroundColor=[UIColor blueColor]; //定義一個導航器來載入第一個視圖控制器,即跟視圖控制器 UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:fViewController]; self.window.rootViewController=navController; return YES;}//第一個視圖控制器類的實現檔案#import "FisrtViewController.h"#import "SecondViewController.h"@interface FisrtViewController ()@end@implementation FisrtViewController- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view. //在跟視圖控制器中添加一個標籤和按鈕 UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 64, 100, 30)]; label.text=@"我是label"; label.textColor=[UIColor redColor]; label.backgroundColor=[UIColor whiteColor]; [self.view addSubview:label]; UIButton *nextBtn=[UIButton buttonWithType:UIButtonTypeCustom]; nextBtn.frame=CGRectMake(220, 64, 100, 30); [nextBtn setTitle:@"nextView" forState:UIControlStateNormal]; [nextBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; nextBtn.backgroundColor=[UIColor redColor]; [nextBtn addTarget:self action:@selector(nextDidClik:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:nextBtn]; UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(30, 144, 240, 296)]; imageView.image=[UIImage imageNamed:@"1.png"]; [self.view addSubview:imageView];}//按鈕觸發時載入到第二個視圖控制器- (void)nextDidClik:(UIButton *)sender{ SecondViewController *secondViewController=[[SecondViewController alloc]init]; secondViewController.view.backgroundColor=[UIColor yellowColor]; [self.navigationController pushViewController:secondViewController animated:YES]; }//第二個視圖控制器類的實現檔案#import "SecondViewController.h"@interface SecondViewController ()@end@implementation SecondViewController- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view. UIButton *beforeBtn=[UIButton buttonWithType:UIButtonTypeCustom]; beforeBtn.frame=CGRectMake(0, 64, 100, 30); [beforeBtn setTitle:@"beforeView" forState:UIControlStateNormal]; [beforeBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; beforeBtn.backgroundColor=[UIColor redColor]; [beforeBtn addTarget:self action:@selector(beforeBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:beforeBtn]; }//按鈕觸發時返回到前一個視圖控制器中- (void)beforeBtn:(UIButton *)sender{ [self.navigationController popToRootViewControllerAnimated:YES];}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.