• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 按下按钮时显示标题而不是段控制ios

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:40:01 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个表格 View ,可以通过工具栏中的按钮加载不同的数据。所以我想做的是隐藏段控制并在按下某个按钮时显示标题,反之亦然,如果按下另一个按钮。

我的段控件被命名为 sortButton,我把它隐藏了

sortButton.hidden = TRUE 

并显示它

sortButton.hidden = FALSE 

所以当按钮被隐藏时,我想在其位置放置标题。知道如何解决这个问题。

我尝试过简单的

self.title = @"Restavracije";

self.navigationItem.title = @"Restavracije"; 

但标题没有出现



Best Answer-推荐答案


我为您制作了一个简约的演示项目,说明了如何完成您想要的。

这是相关代码:

@interface HASTableViewController ()
@property (strong, nonatomic) UISegmentedControl *sortButton;
@property (copy, nonatomic) NSArray *dataSource1;
@property (copy, nonatomic) NSArray *dataSource2;
@property (copy, nonatomic) NSArray *dataSource3;
@end

@implementation HASTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Create the segmented control
    self.sortButton = [[UISegmentedControl alloc] initWithItems[@"First", @"Second", @"Hide me"]];
    [self.sortButton addTarget:self actionselector(switchDataInTableView) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = self.sortButton;
    self.sortButton.selectedSegmentIndex = 0;
    // We set the title you want to show when the segmented control is "hidden"
    self.navigationItem.title = @"Sort Button is nil.";
    // Setup a data source
    self.dataSource1 = @[@"First", @"First", @"First", @"First", @"First", @"First", @"First", @"First", @"First", @"First", @"First"];
    // and another one
    self.dataSource2 = @[@"Second", @"Second", @"Second", @"Second", @"Second"];
    // Create a third datasource which contains both arrays
    NSMutableArray *tempDataSourceArray3 = [[NSMutableArray alloc] initWithArray:self.dataSource1];
    [tempDataSourceArray3 addObjectsFromArray:self.dataSource2];
    self.dataSource3 = tempDataSourceArray3;
}

- (void)switchDataInTableView {
    // Reload the table view.
    // tableView:cellForRowAtIndexPath decides which datasource to show
    [self.tableView reloadData];
    // If it is "Hide it" we hide
    if (self.sortButton.selectedSegmentIndex == 2) self.navigationItem.titleView = nil;
}

#pragma mark - UITableView Data Source Methods

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {
    // We use the standard cell
    UITableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier"Cell" forIndexPath:indexPath];
    // If "First" is selected we want the text to be taken fromt the dataSource1 array
    tableViewCell.textLabel.text = self.sortButton.selectedSegmentIndex == 0 ? self.dataSource1[indexPath.item] : self.sortButton.selectedSegmentIndex == 1 ? self.dataSource2[indexPath.item] : self.dataSource3[indexPath.item];
    return tableViewCell;
}

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section {
    // return number of rows
    return self.sortButton.selectedSegmentIndex == 0 ? self.dataSource1.count : self.sortButton.selectedSegmentIndex == 1 ? self.dataSource2.count : self.dataSource3.count;
}

@end

Download it here

关于ios - 按下按钮时显示标题而不是段控制ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20173708/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap