I have many buttons named like this:
#property (weak, nonatomic) IBOutlet UIButton *Round1Num1;
#property (weak, nonatomic) IBOutlet UIButton *Round1Num2;
#property (weak, nonatomic) IBOutlet UIButton *Round1Num3;
#property (weak, nonatomic) IBOutlet UIButton *Round1Num4;
#property (weak, nonatomic) IBOutlet UIButton *Round2Num1;
#property (weak, nonatomic) IBOutlet UIButton *Round2Num2;
#property (weak, nonatomic) IBOutlet UIButton *Round2Num3;
#property (weak, nonatomic) IBOutlet UIButton *Round2Num4;
and so on.
I was wondering if I could access them dynamically using stringWithFormat or a similar method.
Example (Sorry if the code is wrong!):
Instead of self.Round1Num1 I could call self.[NSString stringWithFormat:#"Round%dNum%d", 1, 1]
You could use -performSelector::
NSString *round2Num1ButtonAccessorSelectorStr = [NSString stringWithFormat:#"Round%dNum%d", 2, 1];
SEL selector = NSSelectorFromString(round2Num1ButtonAccessorSelectorStr);
if ([self respondsToSelector:selector])
UIButton *round2Num1Button = [self performSelector:selector];
For context, [self performSelector:#selector(someSelector)] is essentially the equivalent to self.someSelector (in the case of a property accessor) which resolves to [self someSelector]. All cases actually call the same runtime function, objc_msgSend(self, someSelector).
Specifically in this context, we're creating a local variable that points to the same reference concealed by the respective IBOutlet property on the VC instance. If the property doesn't exist, then neither will the selector (most likely) so you need to safeguard from an unrecognized selector exception via -respondsToSelector:.
Related
I have an array of UIButton objects (I have 5 buttons so I wanted to store them in an array for easy processing). But Array give me error
"Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSArray0 addObject:]:
unrecognized selector sent to instance"
#property (weak, nonatomic) IBOutlet UIButton *starOne;
#property (weak, nonatomic) IBOutlet UIButton *starTwo;
#property (weak, nonatomic) IBOutlet UIButton *starThree;
#property (weak, nonatomic) IBOutlet UIButton *starFour;
#property (weak, nonatomic) IBOutlet UIButton *starFive;
#property (nonatomic, copy) NSMutableArray *_starButtons;
I have below code in viewDidLoad method
- (void)viewDidLoad {
self._starButtons=[[NSMutableArray alloc]init];
[self._starButtons addObject:self.starOne];
[self._starButtons addObject:self.starTwo];
[self._starButtons addObject:self.starThree];
[self._starButtons addObject:self.starFour];
[self._starButtons addObject:self.starFive];
NSLog(#"%#",self._starButtons);
}
Please help me where i am going wrong.
First remove copy from declaration of array property, make it strong.
Second thing as you have said in comment that you have programmatically created buttons then you not need IBOutlets. So, remove IBOutlets from all properties of button.
Your declaration should like,
#property (weak, nonatomic) UIButton *starOne;
#property (weak, nonatomic) UIButton *starTwo;
#property (weak, nonatomic) UIButton *starThree;
#property (weak, nonatomic) UIButton *starFour;
#property (weak, nonatomic) UIButton *starFive;
#property (nonatomic, strong) NSMutableArray *_starButtons;
Try this:
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIButton *one;
#property (weak, nonatomic) IBOutlet UIButton *two;
#property (weak, nonatomic) IBOutlet UIButton *three;
#end
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *buttonArray = [[NSMutableArray alloc] initWithObjects:one,two,three,nil];
NSLog(#"%#",buttonArray);
}
If you're using Storyboard or Nib file then:
Another approach is to connect the UIButtons' to an NSArray of IBOutletCollection in Interface Builder, instead of adding manually each button to an NSMutableArray. Something like this for all UIButtons'.
#property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttonsArray;
Just one change in code.
Replace this line:
#property NSMutableArray *_starButtons;
My app crashes with this stack trace:
[DictationDetailsController respondsToSelector:]: message sent to deallocated instance
I tracked that on instruments trying to see the relevant code causing the crash:
here is the relevant code for MyDictationController in the didSelectRowAtIndexPath: delegate method:
- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath {
DictationDetailsController *controller = GET_CONTROLLER_WITH_CLASS([DictationDetailsController class]);
controller.dictation = [unSubmittedDictations objectAtIndex:indexPath.row];
controller.isEditMode = YES;
controller.selectedDate = _selectedDate;
controller.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:controller animated:YES];
}
#property (copy ,nonatomic) Dictation *dictation;
Also i have used #synthesize. Help me out in this issue, to get which deallocated method is being called.?
Here's my DictationDetailsController interface:
#interface DictationDetailsController : BaseController
#property (copy ,nonatomic) Dictation *dictation;
#property (nonatomic) BOOL isEditMode;
#property (nonatomic) NSDate *selectedDate;
#property (weak, nonatomic) IBOutlet UILabel *navigationTitleLabel;
#property (weak, nonatomic) IBOutlet UITextField *patientNameTextField;
#property (weak, nonatomic) IBOutlet UITextField *accountIDTextField;
#property (weak, nonatomic) IBOutlet UITextField *workTypeTextField;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *deleteButtonWidth;
#property (weak, nonatomic) IBOutlet UIView *tutorialView;
#property (weak, nonatomic) IBOutlet UIView *audioContainer;
#property (weak, nonatomic) IBOutlet UISlider *audioSlider;
#property (weak, nonatomic) IBOutlet UILabel *durationLabel;
#property (weak, nonatomic) IBOutlet UILabel *noRecordingLabel;
#property (weak, nonatomic) IBOutlet UIButton *playPauseButton;
#end
And in dealloc method:
- (void)dealloc {
[player pause];
player = nil;
self.dictation = nil;
}
My guess is the issue is somewhere inside GET_CONTROLLER_WITH_CLASS method. Pop a breakpoint on that line and step over it. It's possibly producing a released instance of the class. That being the case, the crash would occur on the line immediately following the call to that method when it tries to access the dictation property.
I'm having a use of undeclared identifier even though that I declared it in the .h and synthesize in the .m.
I had another problem before, but I posted a question in stack overflow and they said that I shouldn't extern them and when I extered them the code gave me an error "ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)" and know I didn't and it's giving me "Use Of Undeclared Identifier" as you can see
.h
#import <UIKit/UIKit.h>
BOOL OFrameIsHidden, XFrameIsHidden;
NSString *topOne, *topTwo, *topThree;
NSString *midOne, *midTwo, *midThree;
NSString *botOne, *botTwo, *botThree;
void hideAll(void);
#interface ViewController : UIViewController
void hideAll(void);
#property (weak, nonatomic) IBOutlet UIImageView *XFrame;
#property (weak, nonatomic) IBOutlet UIImageView *OFrame;
#property (weak, nonatomic) IBOutlet UIImageView *frame;
#property (weak, nonatomic) IBOutlet UILabel *X;
#property (weak, nonatomic) IBOutlet UILabel *O;
#property (weak, nonatomic) IBOutlet UILabel *WhoWon;
#property (weak, nonatomic) IBOutlet UIButton *oneOne;
#property (weak, nonatomic) IBOutlet UIButton *oneTwo;
#property (weak, nonatomic) IBOutlet UIButton *oneThree;
#property (weak, nonatomic) IBOutlet UIButton *twoOne;
#property (weak, nonatomic) IBOutlet UIButton *twoTwo;
#property (weak, nonatomic) IBOutlet UIButton *twoThree;
#property (weak, nonatomic) IBOutlet UIButton *threeOne;
#property (weak, nonatomic) IBOutlet UIButton *threeTwo;
#property (weak, nonatomic) IBOutlet UIButton *threeThree;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize XFrame, OFrame, frame, X, O, WhoWon;
#synthesize oneOne, oneTwo, oneThree;
#synthesize twoOne, twoTwo, twoThree;
#synthesize threeOne, threeTwo, threeThree;
void hideAll(void){
[OFrame setHidden:YES];
[XFrame setHidden:YES];
[frame setHidden:YES];
[X setHidden:YES];
[O setHidden:YES];
[oneOne setHidden:YES];
[oneTwo setHidden:YES];
[oneThree setHidden:YES];
[oneOne setHidden:YES];
[twoTwo setHidden:YES];
[twoThree setHidden:YES];
[threeOne setHidden:YES];
[threeTwo setHidden:YES];
[threeThree setHidden:YES];
}
For your information there is some more code for the IBAcions, but I don't want to make this long.
Don't mix functions and methods like this. It doesn't really help you and all you have done is create visibility issues.
Methods work by passing a hidden parameter which gives access to self. All of your #property definitions are instance variables, so you need access to self to get to them. Functions, like your hideAll, don't have this access so they can't get to the instance variables (they don't know what an instance is).
You generally also don't want variables defined outside the class definition.
Bring your variables into the class or move them to another more appropriate class and use methods for your code, not functions.
I have a custom view - DetailSubView.h with labels
#property (retain, nonatomic) UILabel *titleLabel;
#property (retain, nonatomic) UILabel *descHeaderLabel;
On custom UICollectionViewCell, i have
#property (strong, nonatomic) DetailSubView *detailView;
When I set text, i received unrecognized selector error.
[self.detailView.titleLabel setText:#"text"];
That's because your titleLabel is declared in DetailView but you're trying to access it from a class of type DetailSubView. It cannot access the getter method for your property on DetailSubView since you don't have it declared there.
My code in my .h file is
#property (weak, nonatomic) IBOutlet UITextField *textFieldTask;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *buttonDone;
and the error shows up
#synthesize of 'weak' property is only allowed in ARC or GC mode
when I replace the weak with strong, the button doesn't work
I can't put it in ARC mode( it will destroy my project)
Anything I can Do?
you need to use retain or assign if you don't want to use ARC
retain
#property (retain, nonatomic) IBOutlet UITextField *textFieldTask;
#property (retain, nonatomic) IBOutlet UIBarButtonItem *buttonDone;
assign
#property (assign, nonatomic) IBOutlet UITextField *textFieldTask;
#property (assign, nonatomic) IBOutlet UIBarButtonItem *buttonDone;