Domanda

When I define the button on .h, it works but when I code the button does not work.

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *buttonImage = [UIImage imageNamed:@"nl.png"];

    //create the button and assign the image
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(230,302,32,32);
    [button setImage:buttonImage forState:UIControlStateNormal];


    //create a UIBarButtonItem with the button as a custom view
    //UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    [button addTarget:self action:@selector(clickActionItem) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

-(void)btnClicked
{
    [self.button Sethidden:YES]:
    self.button.hidden=YES;
    _button.hidden=YES;
}

But not work

È stato utile?

Soluzione 2

@implementation YourView{
    UIButton *button;
}
- (void)viewDidLoad
{
    [super viewDidLoad];

            UIImage *buttonImage = [UIImage imageNamed:@"nl.png"];

            //create the button and assign the image
            button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame=CGRectMake(230,302,32,32);
            [button setImage:buttonImage forState:UIControlStateNormal];


            //create a UIBarButtonItem with the button as a custom view
            //UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc]       initWithCustomView:button];

            [button addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:button];
}

-(void)btnClicked
{
    [button setHidden:YES]; 
}

Your code did not give sufficient information for to see what you intend to do. Can you also provide the header file?

Altri suggerimenti

Issue

The problem is that you are creating UIButton on viewDidLoad Method and you don't keep the instance stored so you cannot access it. Another issue is that you are giving another method to selector of the button.

Solution

Change your target at selector and add the UIButton as parameter just like the code below

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

And the method btnClicked take the instance of the button which is passed at button

-(void)btnClicked:(UIButton *)button
{
    [button sethidden:YES];
}

This should do the trick.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top