Domanda

Ho due class ..

in uno di essi Sto usando sei tasti con la loro etichetta ..

come questo

[btnF1 setTag:1];

e sto usando questo tag

come questo ..

- (void)showPhoto:(UIButton*)btnSender       {    
    NSLog(@"showPhoto:%d",btnSender.tag);   
}  

Ora voglio fare variabile che memorizza il valore di btnSender.tag e posso anche utilizzare tale valore in un altro ramo anche ...

Ho fatto due approches .. 1) Feci una variabile in questa classe che memorizza il valore btnSender.tag e ho cercato di utilizzare questo valore in altra classe ma mi sta dando nullo

2) ho provato la variabile globale, ma non in grado di vedere il valore della variabile globale nel NSLog ..

Non sto usando IB

darmi una mano ...:)

È stato utile?

Soluzione

You can use NSUserDefaults or UILabel to store the value of button tag. But for using UILabel you will have to Outlet it to a hidden label in your XIB only then it will retain its value.

NSUserDefaults stores the value permanently till you refresh it. So there won't be a problem of retaining the values.

So better approach is to use NSUserDefaults as shown below. May be you can put this on an event (for Example ButtonClick if you are going to next view on a button click.)

NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults]; 
[userDef setObject:[NSString stringWithFormat:@"%d",btnSender.tag] forKey:@"BtnTag"];

Now when you go to next view, you can access it using below code:

int tagValue = [[[NSUserDefaults alloc] valueForKey:@"BtnTag"] intValue];

Hope this helps you.

Altri suggerimenti

@interface urViewController : UIViewController 

{
 int buttonTag;
}
@property int buttonTag;

There's only one object that should be responsible for dealing directly with the objects (buttons and anything else) in your view, and that's the view controller. If some other object wants to change the label on a button, disable a button, or otherwise modify a button, it should do so by talking to the view controller. That might seem a roundabout way to do things when you can just access the button directly, but...

  • It completely solves your problem with sharing tags. Nobody but the view controller needs to care about tags.
  • Concentrating responsibility for everything in the view makes your code less buggy and much more maintainable.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top