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



我正在使用Cocoa Touch创建一个单视图应用程序。我需要一个菜单,为单个视图选择不同的主题,我一直在想什么是最好的方法,以及如何实现这一点。

我应该创建一个主细节视图吗?如果是这样的话,我该如何将详细视图作为应用程序加载的初始屏幕。但我不确定这是否是最好的方法。

我也一直在研究这种弹出菜单之类的东西,但我宁愿自己学习如何实现这一点,而不仅仅是购买现成的解决方案。Cocoa Touch中有提供类似功能的类吗?很明显,他们使用Core Graphics从头开始构建了这种菜单,但有没有更简单的方法来实现这种类型的菜单,例如使用一组UIButtons?

代码示例将不胜感激,但我真的在寻找解决这个问题的最佳方法,所以我知道应该熟悉哪些框架。

TIA

您可以尝试将UICollectionViewUICollectionViewFlowLayout结合使用,以创建一个可用于在不同主题之间切换的按钮网格。

如果不了解更多关于你想要从选择菜单中选择什么、你有多少主题、你希望它如何显示等的信息,很难建议一种方法而不是另一种方法。但由于UICollectionViewFlowLayout是苹果公司提供的预定义UICollectionViewLayout,用于以网格排列方式显示UIViews,因此这可能是解决此问题的好方法。

以下是如何使用UICollectioViewFlowLayout:实现UICollectionView

头文件

@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);
}

使用UINavigationController并在导航堆栈上推送带有主题列表的UITableViewController怎么样?

你可以提供的主题数量不受限制,而且应该很容易做到

或者,您可以在将modalTransitionStyle设置为UIModalTransitionStyleFlipHorizontal的情况下使用[UIWiewController presentViewController:animated:completion:]。

如果主题的数量小于或等于6,则可以使用带有几个按钮的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...
  }
}

将注释替换为切换到所选主题。

注意:如果你有六个以上的主题,你会想要一些更像在行动表中UIPickerViews上的SO问题中讨论的解决方案。

有很多方法可以做到这一点。例如将UITableView添加到UIActionSheet并显示特定按钮上的actionSheet。UIPopOverViewController也最适合做这类工作。

以下是可能对您的情况有用的代码来源:

https://github.com/nacho4d/Accordion

https://github.com/kyoshikawa/ZPopoverController

最新更新