【问题标题】:Subviews and buttons子视图和按钮
【发布时间】:2012-07-31 17:15:23
【问题描述】:

我有一个主视图控制器:

@interface ViewController : UIViewController

我的 ScreenView 有一个属性:@property (strong,nonatomic) MUIScreenView* screenView;

它会被调用:

- (void)viewDidLoad
{
    [super viewDidLoad];   
    [self.view addSubview:_screenView];
}

现在看看我的 MUIScreenViewClass:

@interface MUIScreenView : MUIView

@interface MUIView : UIView

在我的 MUIScreenView 中,我有一个方法:

-(void)addScreenObjectToView
{
   MUIButton *button = [[MUIButton alloc] initWithButtonModel:(ButtonModel*)MUIElement 
   [self addSubview:button];

   UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
   myButton.frame = CGRectMake(10, 220, 300, 40);
   [myButton setTitle:@"This is Standard UIButton!" forState:UIControlStateNormal];
   [myButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
   [self addSubview:myButton];

}

这是我绘制东西的 MUIButton 方法:

-(void)addButtonToView
{
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(10, 50, 300, 40);
    [myButton setTitle:@"This is a MUIButton!" forState:UIControlStateNormal];
    [myButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:myButton];
}

最后我有:

顶部按钮怎么可能死了?如果我碰它,什么也没有发生。我以同样的方式创建了它。

总结 - 我目前显示为“这是一个 MUIButton!”是一个视图包含另一个视图,在第二个视图中我有 UIButton。 作为“这是一个标准的 UIButton!”是一个包含 UIButton 的 View。

【问题讨论】:

    标签: objective-c ios uiview uiviewcontroller uibutton


    【解决方案1】:

    您需要将对主视图控制器的视图的引用传递给子视图,然后将按钮添加到该视图。您不能在不同的视图上调用选择器并让正确的视图接收它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2015-03-09
      • 2014-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多