0

哪个更快?

@interface ViewController : UIViewController
{

UIView *myView;

}

@end

@implementation ViewController

 - (void)myInit(){

UIView *view = [[UIView alloc]init];

myView = view;

view.tag = 1;

}

- (void)viewDidLoad(){
[self myInit];

[self.view addSubview:myView];//accessing by variable

//OR

[self.view addSubView:[self.view viewWithTag:1];//accessing by tag

}

@end
4

2 回答 2

3

在一个你有一个直接的指针访问,在另一个你需要调用一个方法,然后找到索引对象并返回它以获得相同的对象。我想你有你的答案

于 2012-10-26T08:42:31.353 回答
0

It's useful to retrieve elements using "viewWithTag" if the elements were hooked to the cell programmatically (i.e. not defined in a NIB and hooked-up via IBOutlets)—this prevents multiple labels etc. to be created for each instance of the cell.Definitely using a variable and /or IBOUtlet in NIBS has the upper merit more ever because when we want to fetch a view via viewWithTag ,the compiler runs a for loop within its subViews and fetch the first view it finds with the given tag.Hence in case where you have two view with same tag ,using viewWithTag always have a major disadvantage.

于 2012-10-26T09:14:53.860 回答