iOS开发基础知识--碎片5

iOS开发基础知识--碎片5 

二十三:addSubview和insertSubview 区别

addSubview 是将view加到所有层的最顶层

相当于将insertSubview的atIndex参数设置成view.subviews count 即

[view addSubview:oneview] == [view insertSubview:oneview atIndex:view.subviews count]

addSubview是加到最后
insertSubview是加到指定的位置

UIView* view=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 30.0, 50.0)];

view.backgroundColor=[UIColor redColor];

[self.view addSubview:view];

UIView* newview=[[UIView alloc] initWithFrame:CGRectMake(30, 0, 40, 50)];

newview.backgroundColor=[UIColor blueColor];

[self.view addSubview:newview];

UIView* twoview=[[UIView alloc] initWithFrame:CGRectMake(0, 50, 100, 200)];

twoview.backgroundColor=[UIColor yellowColor];

[self.view addSubview:twoview];

//第一个从0开始索引   

UIView* aview=[[UIView alloc] initWithFrame:CGRectMake(20, 20, 120, 200)];

aview.backgroundColor=[UIColor grayColor];

[self.view insertSubview:aview atIndex:2];

二十四:loaded the "XXXView" nib but the view outlet was not set 解决方案 

‘-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "XXXView" nib but the view outlet was not set.‘

查书才知道,没有做nib文件到xxxViewControler程序的关联,特此记录下来:

1, 打开nib文件

2, 点击"File‘s Owner", 按command+4,设置Class为xxxViewControler

3, 按Control+"Files‘s Owner", 里面有个默认的IBOutlet变量view, 看一下后面有没有做关联,如果没有就拉到下面的View和视图做个关联

二十五:UIViewController和UIView关系

UIViewController是视图控制器,UIView是视图,UIViewController是来控制UIView的。UIViewController就像是一个容器,它可以对其内部的对象进行操作,UIView是容器中的对象,一个容器里面可以有好多对象;
UIView工作在第一线,向用户展示表现的内容,并接受用户的交互。UIViewController相当于导演,按照计划编排属下的UIView以何种形式展现,其中包含一些花哨的技巧,如翻转、淡入淡出等。
UIVewController的另一个功能是处理用户交互操作,注意这里我说的是"处理",当然UIViewController本身是不知道用户交互的,这就需要UIView将用户的交互操作(例如:touchesBegintouchesMoved)传递上来。一般常用的两种方法完成这种传递:
1、[self nextResponder] touchesBegin:touches...
2、使用Notification 

不管以何种方式,如果UIViewController得到了用户的输入,那么它应该对UIView做些改变以响应用户的输入,这里要注意UIViewController应该只改变UIView的表现,而不应该处理其它事情,
更多的操作通过Delegate来进行,关于Delegate的应用场合下次讲解消息的传递方式中一起阐述。

二十六:页面传值问题

假如要从A传到B页,可以很简单直接在B中定义一个属性,然后在页面跳转前给它赋值,实现跳转;

UserInfo* userModel=[[UserInfo alloc] init];

userModel.userName=self.textBox1.text;

userModel.age=25;

userModel.sex=YES;

//TwoController* twocroller=[[TwoController alloc]init];
   别这么写,或者传过去为空,因为视图间没有什么联系
TwoController* twocroller= [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]

instantiateViewControllerWithIdentifier:@"TwoController"];
    //把B页的视图定义一个标识TwoController
twocroller.newuserinfo = userModel;

[self presentViewController:twocroller animated:YES completion:nil];

如果从A跳到B页,然后再从B页传值给A页,就可以使用委托的方式;

二十七:集合视图跟表视图

集合视图跟表视图的一些概念是不一样,比如节;实现委托的方法也是不一样;

二十八:通过Segue标识进行跳转

[self performSegueWithIdentifier:@"twoSegue" sender:self];
其中push不能直接从ViewController跳转到ViewController;

二十九:UIScreen(屏幕)、UIWindow(画框)、UIView(画布)、

didFinishLaunchingWithOptions的概念

//didFinishLaunchingWithOptions 方法:顾名思义。在app开始运行时会调用里面的方法。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //返回的是带有状态栏的矩形
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    CGRect bound = [[UIScreen mainScreen]bounds];  //返回的是带有状态栏的Rect
    NSLog(@"boundwith:%f    boundheight:%f",bound.size.width,bound.size.height);
    NSLog(@"boundx:%f    boundy:%f",bound.origin.x,bound.origin.y);
    //2012-08-03 23:21:45.716 DinkMixer[599:c07] boundwith:320.000000    boundheight:480.000000
    //2012-08-03 23:21:45.719 DinkMixer[599:c07] boundx:0.000000    boundy:0.000000

    CGRect appBound = [[UIScreen mainScreen]applicationFrame];  //返回的是不带有状态栏的Rect
    NSLog(@"appBoundwith:%f    boundheight:%f",appBound.size.width,appBound.size.height);
    NSLog(@"appBoundx:%f    boundy:%f",appBound.origin.x,appBound.origin.y);
    //2012-08-03 23:21:45.720 DinkMixer[599:c07] appBoundwith:320.000000    boundheight:460.000000
    //2012-08-03 23:21:45.720 DinkMixer[599:c07] appBoundx:0.000000    boundy:20.000000

    //很明显状态栏占用了空间20像素

    MasterViewController *masterViewController = [[[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil] autorelease];//根据nib文件的名称来创建一个视图控制器

    self.navigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];//创建一个导航控制器,并指定该导航控制器的根视图控制器为上面建立的masterViewController

    self.window.rootViewController = self.navigationController;//窗体(window)有一个根视图控制器——这个视图控制器负责配置当窗体显示时最先显示的视图。要让你的视图控制器的内容显示在窗体中,需要去设置窗体的根视图控制器为你的视图控制器。

    [self.window makeKeyAndVisible];//这行代码会让包含了视图控制器视图的Window窗口显示在屏幕上。
    return YES;
}

三十:各种数据类型的@property属性

A.内存管理

NSString: copy

基本数据类型、结构体(int, CGFloat, BOOL, CGRect等):assign

对象(如自定义model类型、NSArray、NSDictionary):strong

控件:weak

其它详细说明:

atomic与nonatomic

atomic的意思就是setter/getter这个函数是一个原语操作。如果有多个线程同时调用setter的话,不会出现某一个线程执行setter全部语句之前,另一个线程开始执行setter情况,相当于函数头尾加了锁一样。
nonatomic不保证setter/getter的原语行,所以你可能会取到不完整的东西。
 比如setter函数里面改变两个成员变量,如果你用nonatomic的话,getter可能会取到只更改了其中一个变量时候的状态,这样取到的东西会有问题。
 如果不需要多线程支持的话,当然nonatomic就够用了,另外由于不涉及锁操作,所以它执行相对快点
atomic:默认是有该属性的,这个属性是为了保证程序在多线程情况,编译器会自动生成一些互斥加锁代码,避免该变量的读写不同步问题。
nonatomic:如果该对象无需考虑多线程的情况,请加入这个属性,这样会让编译器少生成一些互斥加锁代码,可以提高效率。

readwrite与readonly

readwrite:这个属性是默认的情况,会自动为你生成存取器。
readonly:只生成getter不会有setter方法。
readwrite、readonly这两个属性的真正价值,不是提供成员变量访问接口,而是控制成员变量的访问权限。

strong与weak

strong:强引用,也是我们通常说的引用,其存亡直接决定了所指向对象的存亡。如果不存在指向一个对象的引用,并且此对象不再显示在列表中,则此对象会被从内存中释放。
weak:弱引用,不决定对象的存亡。即使一个对象被持有无数个弱引用,只要没有强引用指向它,那么还是会被清除。 指针置为nil;
strong与retain功能相似;weak与assign相似,只是当对象消失后weak会自动把指针变为nil;

assign、copy、retain

assign:默认类型,setter方法直接赋值,不进行任何retain操作,不改变引用计数。一般用来处理基本数据类型。
retain:释放旧的对象(release),将旧对象的值赋给新对象,再令新对象引用计数为1。我理解为指针的拷贝,拷贝一份原来的指针,释放原来指针指向的对象的内容,再令指针指向新的对象内容。
copy:与retain处理流程一样,先对旧值release,再copy出新的对象,retainCount为1.为了减少对上下文的依赖而引入的机 制。我理解为内容的拷贝,向内存申请一块空间,把原来的对象内容赋给它,令其引用计数为1。对copy属性要特别注意:被定义有copy属性的对象必须要 符合NSCopying协议,必须实现- (id)copyWithZone:(NSZone *)zone方法。
 也可以直接使用:

三十一:隐藏Status bar(状态栏)、NavigationBar(导航栏)、tabBarController(标签栏)

隐藏Status bar(状态栏)
[[UIApplication sharedApplication] setStatusBarHidden:YES];

隐藏NavigationBar(导航栏)
[self.navigationController setNavigationBarHidden:YES animated:YES];

隐藏tabBarController(标签栏)   尺寸改成大于480就OK。
[self.tabBarController.view setFrame:CGRectMake(0, 0, 320, 520)];

三十二:不错网站收藏

http://www.baigoogledu.com

http://nshipster.cn/

http://objccn.io/

https://github.com/Xuzz/newsyc

https://github.com/mmackh/Hacker-News-for-iOS

时间: 2024-08-02 11:02:46

iOS开发基础知识--碎片5的相关文章

iOS开发基础知识--碎片32

 iOS开发基础知识--碎片32 1:动画属性UIViewAnimationOptions说明 a:常规动画属性设置(可以同时选择多个进行设置) UIViewAnimationOptionLayoutSubviews:动画过程中保证子视图跟随运动. UIViewAnimationOptionAllowUserInteraction:动画过程中允许用户交互. UIViewAnimationOptionBeginFromCurrentState:所有视图从当前状态开始运行. UIViewAnimat

iOS开发基础知识--碎片1

iOS开发基础知识--碎片1  一:NSString与NSInteger的互换 NSInteger转化NSString类型:[NSString stringWithFormat: @"%d", NSInteger]; NSString转化 NSInteger类型:NSInteger = [NSString intValue]; *其它几个同理 [NSString boolValue].[NSString floatValue].[NSString doubleValue] 二:Obje

iOS开发基础知识--碎片3

iOS开发基础知识--碎片3  iOS开发基础知识--碎片3 十二:判断设备 //设备名称 return [UIDevice currentDevice].name; //设备型号,只可得到是何设备,无法得到是第几代设备 return [UIDevice currentDevice].model; //系统版本型号,如iPhone OS return [UIDevice currentDevice].systemVersion; //系统版本名称,如6.1.3 return [UIDevice

iOS开发基础知识--碎片2

iOS开发基础知识--碎片2 六:获得另一个控件器,并实现跳转 UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UIViewController *registerViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"registerView

iOS开发基础知识--碎片23

iOS开发基础知识--碎片23  1:关于UITableView中关于行重复加载的问题 在Cell里重写prepareForReuse,对一些控件进行清空: 比较简单: -(void)prepareForReuse{ [super prepareForReuse]; _content_label.text = nil; _time_date_label.text = nil; _name_label.text = nil; _career_label.text = nil; } 下面这个是我在c

iOS开发基础知识--碎片21

iOS开发基础知识--碎片21  1:[UIScreen mainScreen].scale知识点 当屏幕分别为640x940时[[UIScreen mainScreen] scale]=2.0 当屏幕分别为320x480时[[UIScreen mainScreen] scale]=1.0 2:如何正确的绘制1像素的线 #define SINGLE_LINE_WIDTH (1 / [UIScreen mainScreen].scale) #define SINGLE_LINE_ADJUST_OF

iOS开发基础知识--碎片24

 iOS开发基础知识--碎片24 1:兼容字体大小6plue跟它以下的区别 #define FONT_COMPATIBLE_SCREEN_OFFSET(_fontSize_) [UIFont systemFontOfSize:(_fontSize_ *([UIScreen mainScreen].scale) / 2)] 在iPhone4~6中,缩放因子scale=2:在iPhone6+中,缩放因子scale=3 运用时: myLabel.font=FONT_COMPATIBLE_SCREEN_

iOS开发基础知识--碎片6

iOS开发基础知识--碎片6  三十三:IOS多视图跳转方法 第一种: 跳转:[self presentModalViewController:control animated:YES]; 返回:[self dismissModalViewControllerAnimated:YES]; 第二种: 跳转:[self.navigationController pushViewController:subTableViewController animated:YES]; 返回:[self.navi

iOS开发基础知识--碎片13

 iOS开发基础知识--碎片13 1:运行程序报the file couldn't be opened because you don't have permission to view it 解决办法:项目—>targets->build settings->build options->changed the value of the "Compiler for C/C++/Objective-C" to Default Compiler. 2:百度地图引用