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;
Related
So I was making an app on Xcode ( Objective C) and I really am not able to find a way to fix it
Can somebody please help me with this?
#import "ViewController.h"
#import "DistanceGetter/DGDistanceRequest.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextField *startLocation;
#property (nonatomic) DGDistanceRequest *req;
#property (weak, nonatomic) IBOutlet UITextField *endLocationA;
#property (strong, nonatomic) IBOutlet UIView *distanceA;
#property (weak, nonatomic) IBOutlet UITextField *endLocationB;
#property (weak, nonatomic) IBOutlet UILabel *distanceB;
#property (weak, nonatomic) IBOutlet UITextField *endLocationC;
#property (weak, nonatomic) IBOutlet UILabel *distanceC;
#property (weak, nonatomic) IBOutlet UIButton *calculateButton;
#end
#implementation ViewController
- (IBAction)calculateButtonTapped:(id)sender {
self.calculateButton.enabled=NO;
self.req = [DGDistanceRequest alloc];
NSString *start = self.startLocation.text;
NSString *destA = self.endLocationA.text;
NSString *destB = self.endLocationA.text;
NSString *destC = self.endLocationA.text;
NSArray *dests = #[destA,destB,destC];
self.req = [self.req initWithLocationDescriptions:dests
sourceDescription:start];
self.req.callback= void(ˆNSArray *responses)
self.distanceC.text=#"callback";
self.calculateButton.enabled = YES;
self.req=nil;
When I make it ˆvoid(NSArray.....) it gives even more errors
You have a typo :
void(^... should be ^void(... - the error is somewhat misleading, and it seems strange than Xcode didn’t suggest a „Fix-it”.
Anyway, I recommend http://goshdarnblocksyntax.com/ to remind yourself with block syntax.
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;
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
#property (weak, nonatomic) IBOutlet UIScrollView *mainScrollView;
#property (weak, nonatomic) IBOutlet UICollectionView *NewPrciousCltnVw;
#property (weak, nonatomic) IBOutlet UICollectionView *NewFashionCltnVw;
+ (NSString *)stringVariable;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;
+ (NSString *)proVariable;
#property (weak, nonatomic) IBOutlet UIScrollView *imageSlider;
#property (weak, nonatomic) IBOutlet UILabel *lblNewArrivals;
#property (weak, nonatomic) IBOutlet UILabel *lblFeaturedProducts;
#property (weak, nonatomic) IBOutlet UILabel *roundedoffers;
#property (weak, nonatomic) IBOutlet UIView *lbloffers;
#property (weak, nonatomic) IBOutlet UIScrollView *offersSlider;
#end
This is my Storyboard
Here ViewController is the 'front-view' of SWRevealViewController and Menu ViewControler is the 'rear-view'.If I continuously use push to SecondViewController and back to ViewController for more than 3 minutes my app gets crashed. Somebody help me out from this issue and thanks in advance
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:.
I declare my .h file like this:
#import <UIKit/UIKit.h>
#interface NavigationTripViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>{
NSArray *questionTitleTrip;
NSArray *questionDescTrip;
NSMutableArray *answerTrip;
NSMutableArray *pickerChoices;
int questionInt;
int totalInt;
IBOutlet UILabel *questionNum;
IBOutlet UILabel *questionTotalNum;
IBOutlet UILabel *recordType;
IBOutlet UITextView *questionDes;
IBOutlet UIView *answerView;
IBOutlet UIButton *preButton;
IBOutlet UIButton *nextButton;
UITextField *text;
UIPickerView *picker;
}
#property (retain, nonatomic) NSArray *questionTitleTrip;
#property (retain, nonatomic) NSArray *questionDescTrip;
#property (retain, nonatomic) NSMutableArray *answerTrip;
#property (retain, nonatomic) NSMutableArray *pickerChoices;
#property (retain, nonatomic) IBOutlet UILabel *questionNum;
#property (retain, nonatomic) IBOutlet UILabel *questionTotalNum;
#property (retain, nonatomic) IBOutlet UILabel *recordType;
#property (retain, nonatomic) IBOutlet UITextView *questionDes;
#property (retain, nonatomic) IBOutlet UIView *answerView;
#property (retain, nonatomic) IBOutlet UIButton *preButton;
#property (retain, nonatomic) IBOutlet UIButton *nextButton;
#property (retain, nonatomic) UITextField *text;
#property (retain, nonatomic) UIPickerView *picker;
-(IBAction)clickPre;
-(IBAction)clickNext;
#end
And my .m file here like this:
#import "NavigationTripViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface NavigationTripViewController ()
#end
#implementation NavigationTripViewController
#synthesize questionTitleTrip,questionDescTrip,answerTripl,pickerChoices,questionNum,questionTotalNum,recordType,questionDes,answerView,preButton,nextButton,text,picker;
All my variables in the #synthesize receive the warnings:
Autosynthesized property 'myVar' will use synthesized instance variable '_myVar', not existing instance variable 'myVar'
Also, the variables and class names used in viewDidLoad don't display in colors, just show in black color. In other view controllers, there are no warnings like this. How to fix these problems?
Edit:
Basically for all intents and purposes you should be building on a new enough XCode to use the new behavior, in that solution you typically will just remove the ivar from the #interface block in the .h file... If for some reason you do need to access an ivar directly you can now declare it in the #implementation block... and use #synthesize var or #synthesize var=_var
OGPost:
to make that go away you can go all new school and drop the iVar, or you can go all old school and add an #synthesize someIvar in your #implementation block.