1

我对 iOS 开发相当陌生。我的要求是我正在设计一个包含 5 个屏幕的应用程序。我有一组 UI 控件(1 个 UIImageView 5 个 UIButtons 就像每个屏幕的标签栏),它们对所有屏幕都是通用的。单击按钮时,仅视图的下半部分需要更改相关细节,而按钮保持不变(类似于窗口中的选项卡控件)。

有没有办法实现这个设计?我可以在多个屏幕之间共享 UI 控件而无需重复代码吗?或者有没有办法在单击按钮时仅更改屏幕的下半部分?

4

1 回答 1

0

你可以有一个单独的类来创建你的 UIControls,然后为每个视图控制器调用适当的方法来获取你想要的 UIControls。

@interface UIControlMaker : NSObject{

    id controlmakerDelegate; // This is so that you can send messages to the viewcontrollers 
}
@property (nonatomic,retain) id controlmakerDelegate; // Release it in dealloc method

- (id)initWithDelegate:(id)delegate;
- (UIView *)createCommonUIControls;

在实现文件中

@implementation UIControlMaker

@synthesize controlmakerDelegate;

- (id)initWithDelegate:(id)delegate{

    if(self = [super init]){
        [self setControlMakerDelegate:delegate];
        return self;
    }else
        return nil;
}

- (UIView *)createCommonUIControls{

      UIView *uicontrolsHolder = [[UIView alloc] initWithFrame:CGRectMake(2,40,320,50)];

      // Create as many uicontrols as you want. It'd be better if you have a separate class to create them

     // Let's create a button for the menuItem
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0 , 0, 50, 35)];
    button.backgroundColor = [UIColor clearColor];
    [button setTitle:@"Button 1" forState:UIControlStateNormal];
    [button addTarget:controlmakerDelegate action:@selector(buttonOnClick) forControlEvents:UIControlEventTouchUpInside];

      [uicontrolsHolder addView:button];
      [button release];

      // Add more uicontrols here

      retun [uicontrolsHolder autorelease];
}

然后在您的视图控制器中创建一个 UIControlMaker 实例并调用 createCommonUIControls 方法,该方法将返回一个您可以添加到您的视图控制器的视图。希望这很清楚。

于 2013-01-16T01:25:24.627 回答