【问题标题】:UIButton not being instantiatedUIButton 没有被实例化
【发布时间】:2014-09-09 08:24:04
【问题描述】:

我正在尝试从已在 Interface Builder 中创建的 UIView 中实例化一个简单的 UIButton,但没有任何反应。 我已将以下代码附加到视图中:

“DetailViewTwo.h”:

#import <UIKit/UIKit.h>

@interface DetailViewTwo : UIView

@property (nonatomic) bool viewIsShown;
@property (nonatomic) float lastY;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) NSMutableArray *radioArray;


-(void)createOption:(NSString *)legenda Value:(NSString *)value action:(SEL)action;
-(void)clearView:(NSString *)novoNome;
-(void)escolherRota:(id)sender;
@end

“DetailViewTwo.m”:

#import "DetailViewTwo.h"
#import "MainViewController.h"
#import <QuartzCore/QuartzCore.h>

@implementation DetailViewTwo

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _viewIsShown=false;
        [self.layer setMasksToBounds:YES];
        self.layer.cornerRadius=6;

        UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        but.frame= CGRectMake(100, 15, 15, 15);
        [but setTitle:@"Ok" forState:UIControlStateNormal];
        [but addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:but];
    }
    return self;
}
@end

(哦,还有,我的 'self.layer.cornerRadius=6;' 也不起作用,有关系吗??)

【问题讨论】:

  • 您是否也在某个地方初始化 DetailViewTwo?如果不是,那么- (id)initWithFrame:(CGRect)frame 将不会被调用。这就是你看不到cornerRadius的原因。要添加按钮,只需将其添加到该视图而不是其父视图。 [self addSubview:but]; 应该添加按钮。如果您仍然看不到按钮,请调整框架!
  • 设置 self.layer.cornerRadius=6;在视图的 drawRect 方法中。

标签: ios objective-c xcode uiviewcontroller uibutton


【解决方案1】:

如果您在情节提要中创建了此视图,则应使用 -initWithCoder: 初始化器而不是 -initWithFrame:。 此外,您应该将按钮添加到 self.view 而不是 superview。

- (id)initWithCoder:(NSCoder *)aDecoder {

    if(self = [super initWithCoder:aDecoder]) {
        //Your code goes here
    }
}

【讨论】:

  • 我想写出完全相同的答案。您还应该考虑使用 -awakeFromNib 作为 UIButton 初始化的位置
【解决方案2】:

你需要将 UIButton 添加到View

[self.superview addSubview:but];

应该是

[self addSubview:but];

【讨论】:

  • [self.view addSubview :but];
  • @LightYagami DetailViewTwo 继承了UIView,所以只使用self
  • 是的,但是detailView里面的View实例还是一个view不是吗?并且名称应为 [self.myviewname addSubview : but];我不太确定,如果我错了,请纠正我
  • @LightYagami我们在继承VC的时候使用self.view,而当我们继承UIView的时候就不需要使用self.view,因为self就是UIview。也可以在 Xcode 中试试这个,你会得到答案。 :)
【解决方案3】:

我有一个在视图中实例化(添加)按钮的解决方案。根据您的代码,您只需将[self.superview addSubview:but]; 替换为此[self.view addSubview:but];

 UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];
                but.frame= CGRectMake(100, 15, 15, 15);
                [but setTitle:@"Ok" forState:UIControlStateNormal];
                [but addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
                [self.view addSubview:but]; // Changes done

【讨论】:

  • 我发布了错误的代码。原来的已经是“self addSubview”了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 2013-09-06
  • 2015-05-20
  • 2016-08-25
  • 2016-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多