美文网首页
自建分段控制器 实现效果

自建分段控制器 实现效果

作者: JaneEyre3X | 来源:发表于2017-09-22 09:48 被阅读0次

    在此之前在 AppDelegate.m 中设置导航才可以完成效果

    ```

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //添加导航控制器

    self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[ViewController new]];

    return YES;

    }

    ```

    // 这个我们的ViewController 在这里实现主要功能

    #import "ViewController.h"

    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

    {

    UITableView * table;

    }

    @end

    @implementation ViewController

    - (void)viewDidLoad {

    [super viewDidLoad];

    self.title = @"小优";

    self.view.backgroundColor = [UIColor grayColor];

    NSArray *array = [NSArray arrayWithObjects:@"唐装",@"男装",@"php",@"金溪",@"java",@"e语言", nil];

    //初始化UISegmentedControl

    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:array];

    segment.tintColor = [UIColor whiteColor];

    //设置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 = @"关闭";

    }

    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) {

    //点击按钮的响应事件;

    }]];

    //弹出提示框;

    [self presentViewController:alert animated:true completion:nil];

    }

    }

    还请书友们 给个评价 让小编知道自己的不足~~谢过啦

    相关文章

      网友评论

          本文标题:自建分段控制器 实现效果

          本文链接:https://www.haomeiwen.com/subject/azvxextx.html