自定义多视图

来源:互联网 发布:新淘宝怎么改收货地址 编辑:程序博客网 时间:2024/04/30 01:58

摘要

       本章主要使用presentViewController和dismissViewControllerAnimated实现自定义多视图的切换,例子里面模拟导航视图控制器模式,不过跳转的时候有点区别。

运行结果




过程概要

1.新建工程后新增一个基于UIViewController的类,用作多视图的第二视图;

2.在主视图里面创建一个UINavigationBar,一个导航栏,控制页面跳转

3.为了增加可视效果,添加了背景图片以及部分文字


主要代码

h文件

////  ViewController.h//  MyNav////  Created by arbboter on 14/12/9.//  Copyright (c) 2014年 arbboter. All rights reserved.//#import <UIKit/UIKit.h>#import "SecondView.h"@interface ViewController : UIViewController{    UINavigationBar* _navigationBar;    UINavigationItem* _navigationItemTitle;    UIBarButtonItem* _barButtonLeft;    UIBarButtonItem* _barButtonRight;    SecondView* _nextView;}@property (nonatomic, retain) UINavigationBar* _navigationBar;@property (nonatomic, retain) UINavigationItem* _navigationItemTitle;@property (nonatomic, retain) UIBarButtonItem* _barButtonLeft;@property (nonatomic, retain) UIBarButtonItem* _barButtonRight;@property (nonatomic, retain) SecondView* _nextView;@end


m文件

////  ViewController.m//  MyNav////  Created by arbboter on 14/12/9.//  Copyright (c) 2014年 arbboter. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize _navigationBar;@synthesize _barButtonLeft;@synthesize _barButtonRight;@synthesize _navigationItemTitle;@synthesize _nextView;- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        // 设置图片    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tree_sky.jpg"]];        CGFloat x = self.view.frame.origin.x;    CGFloat y = self.view.frame.origin.y + 20;    CGFloat w = self.view.frame.size.width;    CGFloat h = 40;    NSLog(@"%.2f %.2f", self.view.frame.size.width, self.view.frame.size.height);        // 设置导航栏    self._navigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(x, y, w, h)];    self._navigationBar.translucent = NO;    self._navigationBar.backgroundColor = [UIColor clearColor];    [self.view addSubview:_navigationBar];        // 导航栏条【左Item,Title,右Item】,分为三部分    self._navigationItemTitle = [[UINavigationItem alloc] initWithTitle:@"平凡之路"];    [self._navigationBar pushNavigationItem:self._navigationItemTitle animated:YES];        self._barButtonRight = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"next.png"] style:UIBarButtonItemStylePlain target:self action:@selector(onNext:)];    [self._navigationItemTitle setRightBarButtonItem:self._barButtonRight animated:YES];        self._nextView = [[SecondView alloc] init];        // 一些界面显示的信息    UITextView* textInfo = [[UITextView alloc] initWithFrame:CGRectMake(x, y+h, w, self.view.frame.size.height-h)];    textInfo.backgroundColor = [UIColor clearColor];    textInfo.editable = NO;    textInfo.textColor = [UIColor redColor];    NSArray* fontList = [UIFont familyNames];    textInfo.font = [UIFont fontWithName:[fontList objectAtIndex:arc4random()%fontList.count] size:18];    textInfo.text = @"            平凡之路 - 朴树\n\n"    "徘徊着的 在路上的\n"    "你要走吗\n"    "易碎的 骄傲着\n"    "那也曾是我的模样\n"    "\n"    "沸腾着的 不安着的\n"    "你要去哪\n"    "谜一样的 沉默着的\n"    "故事你真的在听吗\n"    "\n"    "我曾经跨过山和大海 也穿过人山人海\n"    "我曾经拥有着一切 转眼都飘散如烟\n"    "我曾经失落失望失掉所有方向\n"    "直到看见平凡才是唯一的答案\n"    "\n"    "当你仍然 还在幻想\n"    "你的明天\n"    "她会好吗 还是更烂\n"    "对我而言是另一天\n"    "\n"    "我曾经毁了我的一切 只想永远地离开\n"    "我曾经堕入无边黑暗 想挣扎无法自拔\n"    "我曾经象你象他象那野草野花\n"    "绝望着 渴望着 哭着笑着平凡着\n";        [self.view addSubview:textInfo];    [textInfo release];}-(IBAction)onNext:(id)sender{    // 跳转到下一个视图    [self presentViewController:_nextView animated:YES completion:nil];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (void)dealloc{    [_navigationBar release];    [_navigationItemTitle release];    [_barButtonLeft release];    [_barButtonRight release];    [_nextView release];    [super dealloc];}@end


工程代码

0 0
原创粉丝点击