【问题标题】:iPhone SDK - UIButton not working in custom viewiPhone SDK - UIButton 在自定义视图中不起作用
【发布时间】:2010-08-25 06:29:25
【问题描述】:

这让我发疯了...我看过一些关于类似问题的帖子,但似乎无法让它发挥作用,所以这里是:

这是一个非常简单的示例,我只是在自定义视图中创建自定义 UIButton,然后为其分配自定义操作以响应触摸事件。当我运行应用程序时,会创建按钮,但当我尝试单击它时没有任何反应。

这是我的自定义类的代码:

#import "MySegmentedControl.h"


@implementation MySegmentedControl
@synthesize button1;

- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {


    CGFloat a = frame.origin.x+5;
    CGFloat b = frame.origin.y+5;
    CGFloat l = frame.size.width-15;
    CGFloat h = frame.size.height-15;

    CGFloat c = a+l;
    CGFloat d = b;

    CGRect frame2 = CGRectMake(a, b, l, h);


    UIButton *mybutton1 = [UIButton buttonWithType:UIButtonTypeCustom];
    mybutton1.frame = frame2;
    [mybutton1 setTitle:@"TEST" forState:UIControlStateNormal];
    mybutton1.titleLabel.textColor = [UIColor blackColor];
    mybutton1.backgroundColor = [UIColor blueColor];

    [mybutton1 addTarget:self action:@selector(action)forControlEvents:UIControlEventTouchUpInside];

    [self setButton1:mybutton1];

    self.userInteractionEnabled = YES;
    button1.userInteractionEnabled = YES;

    [self addSubview:button1];

    [self bringSubviewToFront:button1];


}

return self;
}

-(void)action {

    [button1 setTitle:@"done" forState:UIControlStateNormal];
    button1.backgroundColor = [UIColor greenColor];
    NSLog(@"Working!");

}

这里是 appDelegate:

#import "MySegmentedControlAppDelegate.h"
#import "MySegmentedControl.h"

@implementation MySegmentedControlAppDelegate

@synthesize window;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    [window makeKeyAndVisible];

    MySegmentedControl *mycontrol = [[MySegmentedControl alloc] initWithFrame:CGRectMake(10,50,135,50)];

    [window addSubview:mycontrol];

    return YES;
}

正如您从自定义类中看到的那样,从我在其他帖子中读到的内容中,我尝试添加一些内容以增加成功的机会:

  • 启用自定义视图用户交互
  • 将 UIButton 置于最前面
  • 确保按钮的框架完全包含在视图的框架中
  • ...

...但是还是不行!...

任何帮助将不胜感激 - 谢谢!

编辑:这里也是自定义视图的标题:

#import <UIKit/UIKit.h>


@interface MySegmentedControl : UIView {

    UIButton *button1;
}

@property (nonatomic, retain) IBOutlet UIButton *button1;

- (void)action;

@end

【问题讨论】:

    标签: iphone custom-controls uibutton


    【解决方案1】:

    你也可以加入自定义视图的头文件吗?

    (很抱歉回答这个问题,我暂时没有足够的声誉)。

    编辑(和真正的答案):

    我已经构建了一个类似于你的项目,我想我已经找到了问题。

    你的按钮框架不好。看看这个模拟器的截图,自定义 UIView 框架是红色的(只需在你的视图构造函数中添加self.backgroundColor=[UIColor redColor];):

    通过使用此代码:

    CGFloat a = frame.origin.x+5;
    CGFloat b = frame.origin.y+5;
    CGFloat l = frame.size.width-15;
    CGFloat h = frame.size.height-15;
    

    您指的是 windows 基础坐标中的框架!所以你的按钮在你的视野之外,它不能处理触摸事件。

    如果你想动态放置你的按钮,你应该使用这个:

    CGFloat a = self.bounds.origin.x+5;
    CGFloat b = self.bounds.origin.y+5;
    GFloat l = self.bounds.size.width-15;
    CGFloat h = self.bounds.size.height-15;
    

    通过使用self.bounds,您可以使用视图基坐标并且它正在工作(见下面的屏幕截图)!

    希望这会有所帮助!

    【讨论】:

    • 嗨 - 我已经编辑了问题并在其末尾添加了头文件。谢谢。
    • 它有效 - 非常感谢!!我会研究苹果关于坐标的文档...无论如何,这是一个很好的教训,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多