OC开发_Storyboard——NaviationController简单例子
一个简单的Navigation的例子,demo里面用到了上一个demo的MVC,可以参考下:http://www.cnblogs.com/daomul/p/4426063.html
建立一个Nav其实灰常简单,通过Editor->embed in ->NaviationController可以直接引入,就不具体介绍了。效果如下:
代码如下:
ShowStatesViewController.m:
1 // 2 // ShowStatesViewController.m 3 // testForNotification 4 // 5 // Created by bos on 15-4-15. 6 // Copyright (c) 2015年 axiba. All rights reserved. 7 // 8 9 #import "ShowStatesViewController.h" 10 11 @interface ShowStatesViewController () 12 13 @property (weak, nonatomic) IBOutlet UILabel *charactersLabel; 14 15 @end 16 17 18 @implementation ShowStatesViewController 19 20 #pragma life of the view 21 22 23 - (void)viewDidLoad { 24 [super viewDidLoad]; 25 // Do any additional setup after loading the view. 26 27 } 28 29 -(void)viewWillAppear:(BOOL)animated 30 { 31 [super viewWillAppear:animated]; 32 33 //when the view appear ,we should update the ui from the new value 34 [self UpdateUI]; 35 } 36 37 #pragma getter and setter 38 -(void)setTestContent:(NSAttributedString *)testContent 39 { 40 _testContent = testContent; 41 //if (self.view.window) { 42 [self UpdateUI]; 43 //} 44 } 45 46 47 #pragma private function 48 -(void)UpdateUI 49 { 50 //push the attributeName into 51 self.charactersLabel.text = [NSString stringWithFormat:@"%lu of all",(unsigned long)[[self getCharactersByAttributeName:NSForegroundColorAttributeName] length]]; 52 } 53 54 -(NSAttributedString *)getCharactersByAttributeName:(NSString *)attributedName 55 { 56 NSMutableAttributedString *character = [[NSMutableAttributedString alloc]init]; 57 58 int index = 1; 59 60 // one by one check the range of the attribute 61 while(index < [self.testContent length]) 62 { 63 NSRange mRange; 64 //the effect of range 65 id value = [self.testContent attribute:attributedName atIndex:index effectiveRange:&mRange]; 66 67 if(value) 68 { 69 //the range of the characters 70 [character appendAttributedString:[self.testContent attributedSubstringFromRange:mRange]]; 71 index = mRange.length + mRange.location; 72 }else{ 73 index++; 74 } 75 } 76 77 return character; 78 } 79 80 81 82 - (void)didReceiveMemoryWarning { 83 [super didReceiveMemoryWarning]; 84 // Dispose of any resources that can be recreated. 85 } 86 87 /* 88 #pragma mark - Navigation 89 90 // In a storyboard-based application, you will often want to do a little preparation before navigation 91 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 92 // Get the new view controller using [segue destinationViewController]. 93 // Pass the selected object to the new view controller. 94 } 95 */ 96 97 @end
ViewController.m
1 //from other segue 2 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 3 { 4 if ([segue.identifier isEqualToString:@"calulate the num"]) { 5 if ([segue.destinationViewController isKindOfClass:[ShowStatesViewController class]]) { 6 ShowStatesViewController *showVC = (ShowStatesViewController *)segue.destinationViewController; 7 showVC.testContent = self.content.textStorage; 8 } 9 } 10 }
注意的是:
1、 getCharactersByAttributeName这个方法是用来:通过属性类型(比如颜色,这里就是用的颜色)来获得具备该属性的文字,当然这个文字是属性化的文字,里面的算法中range是作为连续的字符是具备该属性的范围
2、UpdateUI主要是用来更新UI界面效果,通过获取该属性字符串的长度返回到第二个界面中显示
3、ViewController.m主要是通过这个界面传递值到 第二个界面,传递是通过segue的,需要通过判断identifier,这里需要在storyBoard中设置对应上。
demo下载:http://pan.baidu.com/s/1dDCkgt7