代码改变世界

Iphone NSMutableArray,NSMutableDictionary AND 动态添加按钮

2012-02-21 17:53  htc开发  阅读(314)  评论(0编辑  收藏  举报

一.NSMutableDictionary 

NSMutableDictionary  * tags;

1.NSMutableDictionary 添加内容:

[tags setValue:xxx forKey :xxx];

2.NSMutableDictionary 遍历:

for(NSString * title in tags){

   //其中得到的title是key

}

3.NSMutableDictionary 根据key取得value:

[tags valueForKey :xxx];


二.NSMutableArray

NSMutableArray *buttons;

1.NSMutableArray添加内容:

[buttons addObject:xxx];

2.NSMutableArray遍历:

for(xxxx *button in buttons){

}

三.动态添加按钮到view以及添加按钮点击事件:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect frame = CGRectMake(0,0,300,20);
[button setFrame:frame];
[button setTitle:@"xxx" forState:UIControlStateNormal];

[button addTarget:self action:@selector(xxxx) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];