【问题标题】:Buttons doesn't work按钮不起作用
【发布时间】:2011-12-02 18:40:12
【问题描述】:

我有UINavigationBar 的按钮,而不是使用自定义视图创建的。我为每个按钮添加了一个带有选择器的目标,但按钮没有响应选择器。你能帮助我吗?

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationItem setTitle:@"Title"];

    // create custom view
    container = [[UIView alloc] init];
    [container setUserInteractionEnabled:TRUE];

    NSString *path;
    // create 2 buttons and put in a custom view
    buttonList = [[UIButton alloc] init];
    path = [[NSBundle mainBundle] pathForResource:@"dotActive" ofType:@"png"];
    UIImage *imgList = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
    path = [[NSBundle mainBundle] pathForResource:@"dotInactive" ofType:@"png"];
    UIImage *imgListPrs = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
    [buttonList setImage:imgListPrs forState:UIControlStateSelected];
    [buttonList setSelected:TRUE];
    [buttonList setFrame:CGRectMake(-30, 0, imgList.size.width, imgList.size.height)];
    [buttonList setImage:imgList forState:UIControlStateNormal];
    [container addSubview:buttonList];
    [buttonList addTarget:self action:@selector(changeType:) forControlEvents:UIControlEventTouchUpInside];
    [buttonList setUserInteractionEnabled:TRUE];


    buttonPic = [[UIButton alloc] init];
    path = [[NSBundle mainBundle] pathForResource:@"dotActive" ofType:@"png"];
    UIImage *imgPic = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
    path = [[NSBundle mainBundle] pathForResource:@"dotInactive" ofType:@"png"];
    UIImage *imgPicPrs = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
    [buttonPic setImage:imgPicPrs forState:UIControlStateSelected];
    [buttonPic setFrame:CGRectMake(-10, 0, imgPic.size.width, imgPic.size.height)];
    [buttonPic setImage:imgPic forState:UIControlStateNormal];
    [container addSubview:buttonPic];
    [buttonPic addTarget:self action:@selector(changeType:) forControlEvents:UIControlEventTouchUpInside];
    [buttonPic setUserInteractionEnabled:TRUE];

    // create UIBarButtonItem with a custom view
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:container];

    // put item on navigation bar
    [self.navigationItem setRightBarButtonItem:item];
}

【问题讨论】:

  • [[UIButton alloc]init] 样式不好 - 请改用工厂方法 [UIButton buttonWithStyle:...]。
  • 那么在这种情况下最好使用:[[UIButton alloc] initWithCustomView:];
  • 如何创建按钮没关系,我还没有找到方法buttonWithStyle:和initWithCustomView:。有 buttonWithType: 和 init:, initWithFrame:, 和 initWithCoder:.

标签: iphone objective-c button uibarbuttonitem uinavigationcontroller


【解决方案1】:

对不起,斯维塔,

我没有看到你的完整代码。检查一下。它会为你工作。

- (void)viewDidLoad{

[super viewDidLoad];

[self.navigationItem setTitle:@"Title"];

    // create custom view
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 30)];
[container setUserInteractionEnabled:TRUE];

    // create 2 buttons and put in a custom view
UIButton *buttonList = [[UIButton alloc] init];
[buttonList setSelected:TRUE];
[buttonList setBackgroundColor:[UIColor redColor]];

[buttonList setFrame:CGRectMake(0, 0, 30.0, 30.0)];
[container addSubview:buttonList];
[buttonList addTarget:self action:@selector(changeType:) forControlEvents:UIControlEventTouchUpInside];
[buttonList setUserInteractionEnabled:TRUE];
[buttonList release];


UIButton *buttonPic = [[UIButton alloc] init];
[buttonPic setFrame:CGRectMake(40.0, 0, 30.0, 30.0)];
[buttonPic setBackgroundColor:[UIColor blueColor]];
[container addSubview:buttonPic];
[buttonPic addTarget:self action:@selector(changeType:) forControlEvents:UIControlEventTouchUpInside];
[buttonPic setUserInteractionEnabled:TRUE];
[buttonPic release];

    // create UIBarButtonItem with a custom view
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:container];

    // put item on navigation bar
[self.navigationItem setRightBarButtonItem:item];
}

- (void)changeType:(id)sender
{
     NSLog(@"Change Type");
}

在 Appdelegate 中添加此代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 
    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];
    [navController release]; 
    return YES;
}

【讨论】:

  • 在这个更新的答案中我没有专注于框架。对不起最后一个答案。 :(
【解决方案2】:

我从来没有以编程方式这样做过。

你能发布你的changeType吗?

可能看起来像

-(void)changeType:(id)sender {
// doing something here
}

并且应该在接口部分声明;

你也可以在它上面设置一个断点,看看它是否调用了。并查看变量的值。请检查是否调用。

编辑:

在我的代码下方,您可以将其插入到默认创建的基于导航的应用程序中:

在 RootViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
    [infoButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *tmpInfoButton = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
    self.navigationItem.rightBarButtonItem = tmpInfoButton;
    [tmpInfoButton release];

}

-(void)buttonPressed:(id)sender {
    NSLog(@"Button pressed...");
}

在 RootViewController.h:

-(void)buttonPressed:(id)sender;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-09
    • 2017-07-29
    • 2013-06-24
    相关资源
    最近更新 更多