【问题标题】:How to use UISegmentControl to change detailViewController of CoreData如何使用 UISegmentControl 更改 CoreData 的 detailViewController
【发布时间】:2011-05-03 02:28:36
【问题描述】:

我已经实现了一个 UISegmentControl 作为我的 detailViewController 的 rightBarButton。 此视图控制器显示从 UITableView 传递的信息。 这个 UITableView 的单元格填充了 CoreData 属性值。

我想要做的是让用户通过 detailViewController 在列表中上下移动。不必让用户不得不返回到 rootViewController,他们将获得通过 UISegmentControl 滚动浏览的能力。

我目前在 detailViewController.m 中有这个

 - (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
 // Setting up UISegmentedControl

// Segmented Control - Custom right bar button with a view
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                        [NSArray arrayWithObjects:
                                         [UIImage imageNamed:@"arrowdown.png"],
                                         [UIImage imageNamed:@"arrowup.png"],
                                         nil]];

[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

segmentedControl.frame = CGRectMake(0, 0, 75, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;

UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];

self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
  }

然后将其附加到以下方法,用于检测被点击的控件。

 - (void)segmentAction:(id)sender
 {
UISegmentedControl* segCtl = sender;
// the segmented control was clicked, handle it here 

if([segCtl selectedSegmentIndex]==0){
    NSLog(@"You clicked the down arrow - the segment clicked was %d", [segCtl selectedSegmentIndex]);
}else {

    NSLog(@"You clicked the up arrow - the segment clicked was %d", [segCtl selectedSegmentIndex]);

}



}

我也很好奇是否有人知道如何检测是否还有可去的地方。也就是说,如果加载的音符在第一个位置,则禁用向下箭头,如果加载的音符在最后一个位置,则禁用向上箭头。这甚至可能吗?

任何帮助将不胜感激。

【问题讨论】:

    标签: iphone objective-c cocoa-touch uisegmentedcontrol


    【解决方案1】:

    我建议您创建一个正式的协议MyDataSource,它提供了访问数据的方法。至少,必须有一种方法来获取指定索引的数据对象和对象的数量。

    在您的 DetailViewController 中,您应该有一个对符合 MyDataSource 的对象的引用。我建议您使用 RootViewController 的实例作为 DetailViewController 的数据源。

    您还应该跟踪当前显示在 DetailViewController 中的对象的索引并适当地更新 UI。

    【讨论】:

    • 那么你的意思是我需要实现一种方法,可以改变当前选中对象的位置并刷新当前视图来表示这种变化?
    • @the0rkus 是的。点击 UISegmentedControl 的按钮后,您应该获取下一个/上一个数据对象并刷新当前视图以显示数据。
    • 当然,说起来容易做起来难。你不会碰巧有什么可以给我看的吗?我已经设置了计数器等,但是在通过我在 NSManagedObject 中使用的 NSArray 排序下一个/上一个时遇到问题。
    • @the0rkus 不,我没有任何源代码可以给你看。请查看Quick Look 框架 - 它的类和协议的作用类似于我所说的。
    • 谢谢,我自己想出来的。虽然它有点坏,但一直在努力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多