OC开发_Storyboard——NaviationController简单例子

一个简单的Navigation的例子,demo里面用到了上一个demo的MVC,可以参考下:http://www.cnblogs.com/daomul/p/4426063.html

建立一个Nav其实灰常简单,通过Editor->embed in ->NaviationController可以直接引入,就不具体介绍了。效果如下:

OC开发_Storyboard——NaviationController简单例子

代码如下:

ShowStatesViewController.m:

 //
// ShowStatesViewController.m
// testForNotification
//
// Created by bos on 15-4-15.
// Copyright (c) 2015年 axiba. All rights reserved.
// #import "ShowStatesViewController.h" @interface ShowStatesViewController () @property (weak, nonatomic) IBOutlet UILabel *charactersLabel; @end @implementation ShowStatesViewController #pragma life of the view - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view. } -(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated]; //when the view appear ,we should update the ui from the new value
[self UpdateUI];
} #pragma getter and setter
-(void)setTestContent:(NSAttributedString *)testContent
{
_testContent = testContent;
//if (self.view.window) {
[self UpdateUI];
//}
} #pragma private function
-(void)UpdateUI
{
//push the attributeName into
self.charactersLabel.text = [NSString stringWithFormat:@"%lu of all",(unsigned long)[[self getCharactersByAttributeName:NSForegroundColorAttributeName] length]];
} -(NSAttributedString *)getCharactersByAttributeName:(NSString *)attributedName
{
NSMutableAttributedString *character = [[NSMutableAttributedString alloc]init]; int index = ; // one by one check the range of the attribute
while(index < [self.testContent length])
{
NSRange mRange;
//the effect of range
id value = [self.testContent attribute:attributedName atIndex:index effectiveRange:&mRange]; if(value)
{
//the range of the characters
[character appendAttributedString:[self.testContent attributedSubstringFromRange:mRange]];
index = mRange.length + mRange.location;
}else{
index++;
}
} return character;
} - (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

ViewController.m

 //from other segue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"calulate the num"]) {
if ([segue.destinationViewController isKindOfClass:[ShowStatesViewController class]]) {
ShowStatesViewController *showVC = (ShowStatesViewController *)segue.destinationViewController;
showVC.testContent = self.content.textStorage;
}
}
}

注意的是:

1、 getCharactersByAttributeName这个方法是用来:通过属性类型(比如颜色,这里就是用的颜色)来获得具备该属性的文字,当然这个文字是属性化的文字,里面的算法中range是作为连续的字符是具备该属性的范围

2、UpdateUI主要是用来更新UI界面效果,通过获取该属性字符串的长度返回到第二个界面中显示

3、ViewController.m主要是通过这个界面传递值到 第二个界面,传递是通过segue的,需要通过判断identifier,这里需要在storyBoard中设置对应上。

demo下载:http://pan.baidu.com/s/1dDCkgt7

上一篇:Ajax的XMLHttpRequest对象


下一篇:使用air进行移动app开发常见功能和问题(二)