ios:ios应用程序中各种屏幕上的相同ui控件

时间:2013-01-16 00:57:25

标签: ios5 uibutton

我是iOS开发的新手。我的要求是我正在设计一个包含5个屏幕的应用程序。我有一组UI控件(1个UIImageView 5 UIButtons,就像每个屏幕的标签栏一样),这些控件对于所有屏幕都是通用的。单击按钮时,只有视图的下半部分需要在按钮保持不变的情况下更改相关详细信息(类似于窗口中的选项卡控件)。

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

1 个答案:

答案 0 :(得分:0)

你可以有一个单独的类来创建你的UIControls然后为每个viewcontroller你调用适当的方法来获得你想要的UIControl。

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

然后在viewcontrollers中创建一个UIControlMaker实例并调用createCommonUIControls方法,该方法将返回一个可以添加到viewcontroller的View。希望很清楚。