在此之前在 AppDelegate.m 中設(shè)置導航才可以完成效果
```
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//添加導航控制器
self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[ViewController new]];
return YES;
}
```
// 這個我們的ViewController 在這里實現(xiàn)主要功能
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
UITableView * table;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"小優(yōu)";
self.view.backgroundColor = [UIColor grayColor];
NSArray *array = [NSArray arrayWithObjects:@"唐裝",@"男裝",@"php",@"金溪",@"java",@"e語言", nil];
//初始化UISegmentedControl
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:array];
segment.tintColor = [UIColor whiteColor];
//設(shè)置frame
segment.frame = CGRectMake(0, 65, 340, 30);
//添加到視圖
[self.view addSubview:segment];
UIButton * but = [[UIButton alloc]initWithFrame:CGRectMake(340, 65, 60, 30)];
[but setTitle:@"+" forState:UIControlStateNormal];
but.backgroundColor = [UIColor whiteColor];
[but addTarget:self action:@selector(dianji:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:but];
}
-(void)dianji:(UIButton *)btn
{
UIView * vieww = [[UIView alloc]initWithFrame:CGRectMake(260, 100, 60, 150)];
vieww.backgroundColor = [UIColor whiteColor];
[self.view addSubview:vieww];
table =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150) style:UITableViewStyleGrouped];
table.delegate = self;
table.dataSource = self;
[vieww addSubview:table];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString * cellID = @"cell";
UITableViewCell * cell =[table dequeueReusableCellWithIdentifier:cellID];
if (!cell)
{
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
if (indexPath.row == 0)
{
cell.textLabel.text = @"添加";
}
if (indexPath.row == 1)
{
cell.textLabel.text = @"確認";
}
if (indexPath.row == 2)
{
cell.textLabel.text = @"關(guān)閉";
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 2;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"確認添加" message:@"操作已完成" preferredStyle:? UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//點擊按鈕的響應(yīng)事件;
}]];
//彈出提示框;
[self presentViewController:alert animated:true completion:nil];
}
}
還請書友們 給個評價 讓小編知道自己的不足~~謝過啦