I have this
UILabel *selectedLabel;
selectedLabel = nil;
if (is_x) {
selectedLabel = labelField_x;
} else if (is_y) {
selectedLabel = labelField_y;
} else if (is_z) {
selectedLabel = labelField_z;
}
To prevent this from repeating in the code, how can I make a method that will return a type UILabel class.
I have tried this (it does’t work):
in header (.h) file:
//new method
- (UILabel *) selected;
in implementation (.m) file:
- (UILabel *) selected {
UILabel *selectedLabel;
selectedLabel = nil;
if (is_x) {
selectedLabel = labelField_x;
} else if (is_y) {
selectedLabel = labelField_y;
} else if (is_z) {
selectedLabel = labelField_z;
}
return selectedLabel;
}
- (IBAction)buttonPressed:(id)sender{
[self selected];
}
How can I return selectedLabel inside the IBAction.
Thank you.
Your code looks right, all you need to do is create a
UILabelvariable in your buttonPressed: method to store the reference to the returned label. So changeto: