使用Cocoa Touch和Objective C创建选择菜单

4
我正在使用Cocoa Touch创建一个单一视图应用程序。我需要一个菜单,其中包含不同主题的选择,用于该单一视图,我一直在思考最佳方法,并想知道如何实现它。
我应该创建一个Master-Detail视图吗?如果是这样,我该如何让详细视图成为应用程序加载的初始屏幕。但是我不确定这是否是最好的方法。
我还看过像这样的东西弹出菜单,但我宁愿学习如何自己实现这种东西,而不是只购买现成的解决方案。 Cocoa Touch中是否有任何类提供类似的功能?他们显然是使用核心图形从头构建了这个菜单,但是是否有更简单的方法来实现这种类型的菜单,例如使用一组UIButtons?
非常感谢代码示例,但我真正寻找的是解决此问题的最佳方法,以便我知道要熟悉哪些框架。
TIA

这个问题非常泛泛。你同时在询问设计和实现。我建议你把它分开来。从设计问题开始。当解决了这个问题后,再进行实现。 - Bored Astronaut
你的问题到底是什么?你是在问如何在应用启动时加载detailView,还是如何像你提到的例子中那样自定义弹出窗口? - zahreelay
我正在寻找在Objective C/Cocoa Touch中实现选择菜单的最佳方法(特别是用于选择不同主题,因此菜单应具有视觉性质),并希望了解如何实现。我应该使用Master-Detail视图吗?UITableView还是UICollectionView?我应该使用ActionSheet,还是应该使用UINavigationController?或者我应该使用其他东西代替它们?由于有这么多不同的方法,我想知道哪种方法最适合我想要实现的目标。 - Jimmery
4个回答

2
您可以尝试使用UICollectionViewFlowLayout来创建一个按钮网格,以便在不同的主题之间进行切换。

如果不知道您对选择菜单的需求、主题数量、显示方式等更多信息,很难建议使用哪种方法。但是因为UICollectionViewFlowLayout是由苹果提供的预定义UICollectionViewLayout,用于以网格形式显示UIViews,所以这可能是解决此问题的好方法。

以下是如何实现具有UICollectioViewFlowLayoutUICollectionView

头文件

@interface ViewController : UIViewController <UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> {
    UICollectionView * themeSelection;
}

实现文件

- (void)viewDidLoad {
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    themeSelection =[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    [themeSelection setDataSource:self];
    [themeSelection setDelegate:self];

    [themeSelection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [self.view addSubview: themeSelection];

    [super viewDidLoad];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 15;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    //Each cell is a theme that could be selected

    return cell;
}     

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(100, 100);
}

1
如果主题数量小于或等于六个,您可以使用带有几个按钮的UIActionSheet。在您的视图控制器中,使用以下代码处理“选择主题”按钮的点击事件:
UIActionSheet* as = [[UIActionSheet alloc] initWithTitle:@"Choose Theme" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Theme 1", @"Theme 2", @"Blue Theme", @"etc."];
[as showInView:[self view]];

在您的.h文件顶部,在类定义旁边添加以下内容:
<UIActionSheetDelegate>

在 .m 文件中添加以下方法:
-(void)actionSheet:(UIActionSheet*)as clickedButtonAtIndex:(NSInteger)index {
  switch(index) {
    case 0:
      //It was theme 1
      break;
    case 1:
      //It was Theme 2
      break;
    case 3:
      //It was blue theme
      break;
    //And so on...
  }
}

将注释替换为选择的主题。

注意:如果您有超过六个主题,您需要类似于this SO问题中讨论的UIPickerViews在操作表中的解决方案。


1
如何使用UINavigationController并将UITableViewController推入导航堆栈中,列表中包含主题?您可以提供任意数量的主题,这样做应该很容易。或者您可以使用[UIViewController presentViewController:animated:completion:],并将modalTransitionStyle设置为UIModalTransitionStyleFlipHorizontal。

有趣。您能详细介绍一下您建议的UINavigationController方法吗? - Jimmery

0

谢谢提供的链接,我会去查看一下。UIPopOverViewController似乎是我需要的好解决方案,但它只适用于iPad,这很遗憾,因为我正在开发适用于iPad和iPhone的应用程序。 - Jimmery

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接