开发者

iphone - hiding UIToolbar issue

开发者 https://www.devze.com 2023-03-30 02:55 出处:网络
I have looked at other answers and wrote the codes like that: -(IBAction)tbutton:(id)sender { tb1 = [[[UIToolbar alloc] init] autorelease];

I have looked at other answers and wrote the codes like that:

-(IBAction)tbutton:(id)sender
{
tb1 = [[[UIToolbar alloc] init] autorelease];
tb2 = [[[开发者_如何转开发UIToolbar alloc] init] autorelease];   

if (tb1.hidden == YES && tb2.hidden == YES)
{
tb1.hidden = NO;
tb2.hidden = NO;
//toolbars implementation codes here//
}

else 
{
tb1.hidden = YES;
tb2.hidden = YES;
//toolbars implementation codes here//
}
}

I dont want to display two toolbars when the app is loaded, but I want the toolbars appear when clicking on UIButton *toolbarbutton´s selector action named tbutton:, but it happens nothing. NSLog showed only "else", not "if"... I haven´t set Boolean value of tb1. and tb2.hidden anywhere, only in this action function.

I also want the toolbars disappear when clicking on the button and the toolbars are already appeared.


EDIT 31 august 2011:

I have followed gamozzii's and Maxner's advice, but I still have same problem. Any solution/suggestion to solve this?


EDIT 16 October 2011:

I think it has something to do with alloc, init and autorelease. I will study them whenever I can and post my answer to solve this problem :)


You are allocating your UIToolbar objects inside your action method? (or is that a typo).

The default value of the hidden property is NO, so after you allocate the UIToolbar it will have a value of 'NO' for hidden - you will need to explicitly set it to YES to change it to hidden.

I suspect you want to allocate your UIToolbar objects inside your viewDidLoad method and set the hidden property to YES there, then have your action method implemented as above but without the UIToolbar allocation lines.

i.e. in header file

IBOutlet UIToolbar *tb1;
IBOutlet UIToolbar *tb2;

@property (nonatomic, retain) IBOutlet UIToolbar *tb1;
@property (nonatomic, retain) IBOutlet UIToolbar *tb2;

implementation file

-(void)viewDidLoad {
        self.tb1 = [[UIToolbar alloc] init] autorelease];
        self.tb2 = [[UIToolbar alloc] init] autorelease];
        self.tb1.hidden = YES;
        self.tb2.hidden = YES;
 }

-(IBAction) tbutton:(id)sender {
   if (tb1.hidden == YES && tb2.hidden == YES)
   {
       tb1.hidden = NO;
       tb2.hidden = NO;
       //toolbars implementation codes here//
   }

   else 
   {
       tb1.hidden = YES;
       tb2.hidden = YES;
       //toolbars implementation codes here//
   }
}


I would recommend using alpha instead of hidden. So tb1.hidden = YES becomes tb.alpha = 0 and hidden = NO becomes alpha = 1.

0

精彩评论

暂无评论...
验证码 换一张
取 消