gpt4 book ai didi

iPhone SDK - UIButton 在自定义 View 中不起作用

转载 作者:行者123 更新时间:2023-12-03 20:58:00 26 4
gpt4 key购买 nike

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

这是一个非常简单的示例,我只是在自定义 View 中创建一个自定义 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;
}

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

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

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

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

编辑:这也是自定义 View 的标题:

#import <UIKit/UIKit.h>


@interface MySegmentedControl : UIView {

UIButton *button1;
}

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

- (void)action;

@end

最佳答案

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

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

编辑(和真实答案):

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

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

alt text

通过使用此代码:

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,您可以使用 View 基坐标并且它正在工作(请参见下面的屏幕截图)!

alt text alt text

希望这有帮助!

关于iPhone SDK - UIButton 在自定义 View 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3563347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com