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.
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;
How to set UILabel text using KVC in iOS ?
Suppose I have following lines of code :
#property (nonatomic, strong) UILabel *nameLabel;
[self setValue:#"someName" forKeyPath:#"nameLabel.text"];
NSLog(#"Using KVC :Output name label - %#",[self valueForKeyPath:#"nameLabel.text"]);
/* Returns nil */
#property (weak, nonatomic) IBOutlet UILabel *nameLabel;
[self setValue:#"Testing123" forKeyPath:#"nameLabel.text"];
See the image below of the storyboard
Seem like I found the fix. UILabel must be on subview, either by dragging and dropping on the storyboard and creating IBOutet or by programatically adding it on the subview.
So, #property (nonatomic, weak) IBOutlet UILabel *nameLabel; did the trick for me.
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:.
so I'm writing Obj-C for iOS, and something "strange" is happening..
I have an MVC, with a UITableView (private):
#interface MVC ()
#property (strong, nonatomic) IBOutlet UITableView *tableView;
#property (strong, nonatomic) CellVC *cell1;
#property (strong, nonatomic) CellVC *cell2;
#end
I load the table view up with a few custom cells. My custom cell class is actually a UIViewController... so I instantiate a few, and set cell.contentView to the corresponding CellVC.view inside the tableView:cellForRowAtIndexPath: method. My custom cell class:
#interface CellVC : UIViewController
#property (strong, nonatomic) IBOutlet UIKnob *knob1;
#property (strong, nonatomic) IBOutlet UIKnob *knob2;
#property (strong, nonatomic) IBOutlet UIKnob *knob3;
#property (strong, nonatomic) IBOutlet UIKnob *knob4;
#end
In case you're wondering, I've written a subclass of UIControl named UIKnob...
In CellVC's viewDidAppear: method, I've set a breakpoint to check the values of every knob. Each knob is non-nil, so I am happy... they have all been created.
My goal is to set MVC as the delegate of each knob. (4 knobs for each cell)
If I set a breakpoint anywhere in MVC, the value of each knob is nil??...
cell1.knob1.delegate = self;
will not work because the knobs only exist inside CellVC.m ...
Any ideas??
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;