I am new to coding and had some basic knowledge but i am building out my first app from a tutorial and have a issue i can't figure out and after a few days of looking figured i would just ask. I get an error in my implementation file when initializing the object in the view did load.
It says use of undeclared identifier. any help would be greatly appreciated.
Here is my view controller.m
#import "ViewController.h"
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"titleLabel.text = %#", self.titleLabel.text);
self.bandObject = [[BandObject alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
self.bandObject.name = self.nameTextField.text;
[self.nameTextField resignFirstResponder];
return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
self.bandObject.name =self.nameTextField.text;
[self saveBandObject];
[self.nameTextField resignFirstResponder];
return YES;
}
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
self.saveNotesButton.enabled = YES;
return YES;
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
self.bandObject.notes = self.notesTextView.text;
[self.notesTextView resignFirstResponder];
self.saveNotesButton.enabled = NO;
return YES;
}
- (IBAction)saveNotesButtonTouched:(id)sender
{
[self textViewShouldEndEditing:self.notesTextView];
}
- (IBAction)ratingStepperValueChanged:(id)sender
{
self.ratingValueLabel.text = [NSString stringWithFormat:#"%g",self.ratingStepper.value];
self.bandObject.rating = (int)self.ratingStepper.value;
}
- (IBAction)tourStatusSegmentedControlValueChanged:(id)sender
{
self.bandObject.touringStatus = self.touringStatusSegmentedControl.selectedSegmentIndex;
}
- (IBAction)haveSeenLiveSwitchValueChanged:(id)sender
{
self.bandObject.haveSeenLive = self.haveSeenLiveSwitch.on;
}
#end
and here is my .h
#import <UIKit/UIKit.h>
#import "WBABand.h"
#interface ViewController : UIViewController <UITextFieldDelegate, UITextViewDelegate>
#property (nonatomic, strong) WBABand *bandObject;
#property (nonatomic, weak) IBOutlet UILabel *titleLabel;
#property (nonatomic, weak) IBOutlet UITextField *nameTextField;
#property (nonatomic, weak) IBOutlet UITextView *notesTextView;
#property (nonatomic, weak) IBOutlet UIButton *saveNotesButton;
#property (nonatomic, weak) IBOutlet UIStepper *ratingStepper;
#property (nonatomic, weak) IBOutlet UILabel *ratingValueLabel;
#property (nonatomic, weak) IBOutlet UISegmentedControl *touringStatusSegmentedControl;
#property (nonatomic, weak) IBOutlet UISwitch *haveSeenLiveSwitch;
- (IBAction)saveNotesButtonTouched:(id)sender;
- (IBAction)ratingStepperValueChanged:(id)sender;
- (IBAction)tourStatusSegmentedControlValueChanged:(id)sender;
- (IBAction)haveSeenLiveSwitchValueChanged:(id)sender;
#end
It is because you have an object in your .h called WBABand. And in your .m your are initializing a BandObject which does not exists.
Change this [[BandObject alloc] init];
to this [[WBABand alloc] init];
When you run into an issue, please try to narrow it down to the shortest code block possible to reproduce. At the very least, you should be pointing out the line that shows the error so that contributors can help you easier.
You are trying to instantiate an instance of BandObject, but the class you should be trying to instantiate is WBABand.
self.bandObject = [[WBABand alloc] init];
Related
I want to pass UITextField *nameText from GreenViewController to UILabel *nameValue in ViewController using delegate.
I made delegate, the onSave method in ViewController is called but the screen is not going back and the name is not passed.
What is the problem?
Here are the header and impelentation files:
GreenViewController.h
#import <UIKit/UIKit.h>
#protocol GreenViewDelegate <NSObject>
-(void)onSave:(NSString*)nameValue;
#end
#interface GreenViewController : UIViewController
#property id<GreenViewDelegate> delegate;
#property (nonatomic) NSString* sliderValuePassed;
#property (weak, nonatomic) IBOutlet UIButton *Next;
#property (weak, nonatomic) IBOutlet UIButton *Save;
#property (weak, nonatomic) IBOutlet UIButton *Back;
#property (weak, nonatomic) IBOutlet UILabel *sliderTextValue;
#property (weak, nonatomic) IBOutlet UITextField *NameText;
- (IBAction)save:(id)sender;
#end
GreenViewController.m
#import "GreenViewController.h"
#import "ViewController.h"
#interface GreenViewController ()
#end
#implementation GreenViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.sliderTextValue.text = self.sliderValuePassed;
}
- (IBAction)back:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)save:(id)sender {
[self.delegate onSave:self.NameText.text];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "GreenViewController.h"
#interface ViewController : UIViewController <GreenViewDelegate>
#property (weak, nonatomic) IBOutlet UISlider *slider;
#property (weak, nonatomic) IBOutlet UILabel *sliderTextValue;
#property (weak, nonatomic) IBOutlet UIButton *EnterName;
#property (weak, nonatomic) IBOutlet UILabel *NameValue;
#property (weak, nonatomic) IBOutlet UIButton *LikeIOS;
#property (weak, nonatomic) IBOutlet UISwitch *CheckboxIOS;
#end
ViewController.m
#import "ViewController.h"
#import "RedViewController.h"
#import "GreenViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.sliderTextValue.text = [NSString stringWithFormat:#"Slider value = %d",(int)self.slider.value];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"yellowToGreen"]){
GreenViewController* gVC = segue.destinationViewController;
gVC.sliderValuePassed = [NSString stringWithFormat:#"Slider value = %d",(int)self.slider.value];
gVC.delegate = self;
}else if([segue.identifier isEqualToString:#"yellowToRed"]){
RedViewController* rVC = segue.destinationViewController;
rVC.sliderValuePassed = [NSString stringWithFormat:#"Slider value = %d",(int)self.slider.value];
rVC.CheckboxIOS = self.CheckboxIOS;
}
}
- (IBAction)sliderChangeValue:(id)sender {
int sliderVal = self.slider.value;
self.sliderTextValue.text = [NSString stringWithFormat:#"Slider value = %d",sliderVal];
}
-(void)onSave:(NSString*)nameValue{
NSLog(#"onSave in father controller");
self.NameValue.text = [NSString stringWithFormat:#"Name = %#",nameValue];
}
#end
I assume you didnt set the delegate .
Set it like this:
GreenViewController *myVc = [[GreenViewController alloc] init];
myVc.delegate = self;
Put this in your view controller view did load method!!!
try with GreenViewController.h:
#property (nonatomic, assign) id<GreenViewDelegate> delegate;
and GreenViewController.m:
- (IBAction)save:(id)sender {
[_delegate onSave:self.NameText.text];
[self.navigationController popViewControllerAnimated:YES];
}
ViewController.* seems to be OK
I am new to iPhone App development, below is ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self updateMyView];
}
- (IBAction)clickButtonResult:(id)sender
{
enteredText = [textField text]; // Or textField.text
NSLog(#"Number 1 : %i", number_1);
NSLog(#"Number 2 : %i", number_2);
NSLog(#"Entered Text is %#", enteredText);
int NUM_RESULT = number_1 + number_2;
verify_result = [NSString stringWithFormat:#"%i", NUM_RESULT];
NSLog(#"Verify Result : %#", verify_result);
NSString *final_result = [NSString stringWithFormat:#"%d", [enteredText isEqualToString:verify_result]];
int final_int_result = [final_result integerValue];
if (final_int_result) {
//result_label.text = #"Correct";
NSLog(#"Correct");
[self updateMyView];
} else {
//result_label.text = #"Wrong";
NSLog(#"Wrong");
}
}
- (int)getRandomNumberBetween:(int)min maxNumber:(int)max
{
return min + arc4random() % (max - min + 1);
}
- (void) updateMyView
{
number_1 = [self getRandomNumberBetween:10 maxNumber:99];
number_2 = [self getRandomNumberBetween:10 maxNumber:99];
num_1.text = [NSString stringWithFormat:#"%i", number_1];
num_2.text = [NSString stringWithFormat:#"%i", number_2];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
and ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UILabel *num_1;
IBOutlet UILabel *num_2;
IBOutlet UILabel *result_label;
IBOutlet UITextField *textField;
int number_1;
int number_2;
NSString *verify_result;
NSString *enteredText;
BOOL display_result;
}
- (IBAction)clickButtonResult:(id)sender;
#end
After entering the correct result the UIView should be updated with updateMyView function but it is not happening.
Can anyone help here??
First of all, start using Properties.
ViewController.h
#interface ViewController : UIViewController
#property (nonatomic, weak) IBOutlet UILabel *num_1;
#property (nonatomic, weak) IBOutlet UILabel *num_2;
#property (nonatomic, weak) IBOutlet UILabel *resultLabel;
#property (nonatomic, weak) IBOutlet UITextField *textField;
#property (nonatomic) NSInteger number_1;
#property (nonatomic) NSInteger number_2;
#property (nonatomic, strong) NSString *verifyResult;
#property (nonatomic, strong) NSString *enteredText;
#property (nonatomic) BOOL displayResult;
- (IBAction)clickButtonResult:(id)sender;
#end
In ViewController.m code use self.{name of property}, for example self.textField for the textField property.
Now, go to Interface builder and connect the IBOutlet properties to the right objects. (click with right button on File's Owner)
Try changing num_1.text in viewDidLoad to make sure you have access to that label from your UIViewController.
So in viewDidLoad, just put something like
num1.text = #"Updated from viewDidLoad"
Make sure that you have connected num_1 and num_2 with the UILabels properly. It seems that you have not connected these.
Just move [self updateMyView]; outside the if statements in clickButtonResult: function
then it will update the view when click the button.
if (final_int_result) {
//result_label.text = #"Correct";
NSLog(#"Correct");
} else {
//result_label.text = #"Wrong";
NSLog(#"Wrong");
}
[self updateMyView];
When I try to change the text of a label on button click, the code compiles but it doesn't work. I click the button and nothing happens.
Currently, im just trying to display "Error" on label x1Label when button solveButton will be pressed. Could you please help me fixing it?
.h
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize errorLabel;
#synthesize aField, bField, cField;
#synthesize x1Label, x2Label;
#synthesize solveButton;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
aField.delegate = self;
bField.delegate = self;
cField.delegate = self;
aField.keyboardType = UIKeyboardTypeNumberPad;
bField.keyboardType = UIKeyboardTypeNumberPad;
cField.keyboardType = UIKeyboardTypeNumberPad;
NSString *str = #"car";
errorLabel.text = str;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[aField resignFirstResponder];
[bField resignFirstResponder];
[cField resignFirstResponder];
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[aField resignFirstResponder];
[bField resignFirstResponder];
[cField resignFirstResponder];
}
- (IBAction)solveButton:(id)sender
{
errorLabel.text = #"Error";
}
#end
.m
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextFieldDelegate>
{
IBOutlet UITextField *aField, *bField, *cField;
}
#property (weak, nonatomic) IBOutlet UILabel *errorLabel;
#property (strong, nonatomic) IBOutlet UITextField *aField;
#property (strong, nonatomic) IBOutlet UITextField *bField;
#property (strong, nonatomic) IBOutlet UITextField *cField;
#property (weak, nonatomic) IBOutlet UILabel *x1Label;
#property (weak, nonatomic) IBOutlet UILabel *x2Label;
#property (weak, nonatomic) IBOutlet UIButton *solveButton;
#end
Thank you
You are writing on errorLabel not on x1Label.
Did you connect the SolveButton action to the button in your storyboard?
if not go to storyboard > click on your button > go to Connection Inspector (on the right panel) and link the action to you button.
I have a simple program where you can type text into a text field, hit an ok button, and a label updates with the text entered.
I want the iPhone keyboard to disappear when I hit the OK button, when I press a large button that is in the background covering the whole view, or when I hit the return button on the keyboard. I have been trying to use the
[textField resignFirstResponder]
method, but it is not working. The program compiles fine, but when this method is called from any one of those events, it stops, and I get a message saying:
Thread 1: signal SIGABRT"
What am I doing wrong?
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize txtName;
#synthesize lblMessage;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)doSomething:(UIButton *)sender
{
[txtName resignFirstResponder];
NSString *msg = [[NSString alloc] initWithFormat:#"Hello, %#", txtName.text];
[lblMessage setText:msg];
//[msg release];
}
- (IBAction)makeKeyboardGoAway:(UIButton *)sender
{
[txtName resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
#end
Here is the header file as well:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *txtName;
#property (weak, nonatomic) IBOutlet UILabel *lblMessage;
- (IBAction)doSomething:(UIButton *)sender;
- (IBAction)makeKeyboardGoAway:(UIButton *)sender;
#end
Well I got it working, but I still don't understand the error message I was getting.
Here is the code that worked for me.
Header:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UITextField *txtName;
IBOutlet UILabel *lblMessage;
}
#property (nonatomic, retain) IBOutlet UITextField *txtName;
#property (nonatomic, retain) IBOutlet UILabel *lblMessage;
- (IBAction)doSomething;
- (IBAction)makeKeyboardGoAway;
#end
Implementation:
#import "ViewController.h"
#implementation ViewController
#synthesize txtName;
#synthesize lblMessage;
- (IBAction)doSomething
{
[txtName resignFirstResponder];
NSString *msg = [[NSString alloc] initWithFormat:#"Hello, %#",
txtName.text];
[lblMessage setText:msg];
//[msg release];
}
- (IBAction) makeKeyboardGoAway
{
[txtName resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Call this method when the OK button is clicked.
[self.view endEditing:YES];
This works....if everything lines up with this it might be something else in your code....
...also be sure to have all objects linked correctly (VC to TextField and Button....Button back to VC)...that could also cause the crash
.h
#interface ViewController : UIViewController {
IBOutlet UIButton *button;
IBOutlet UITextField *textfield;
IBOutlet UILabel *label;
NSString *string;
}
-(IBAction)dismiss:(id)sender;
#property (nonatomic, retain)UIButton *button;
#property (nonatomic, retain)UITextField *textfield;
#property (nonatomic, retain)UILabel *label;
.m
#synthesize textfield, button, label;
-(IBAction)dismiss:(id)sender {
[textfield resignFirstResponder];
string = [NSString stringWithFormat:#"Hello %#", textfield.text];
[label setText:string];
}
I have a a class I created to generate UIButton's I add to my UIView. This worked great until my conversion to ARC yesterday, not I get the following error:
-[OrderTypeButton performSelector:withObject:withObject:]: message sent to deallocated instance 0x12449f70
Here is the code to add the button to my UIView (actually a subview in my main UIView):
OrderTypeButton *btn = [[OrderTypeButton alloc]initWithOrderType:#"All Orders" withOrderCount:[NSString stringWithFormat:#"%i",[self.ordersPlacedList count]] hasOpenOrder:NO];
btn.view.tag = 6969;
btn.delegate = self;
[btn.view setFrame:CGRectMake((col * width)+ colspacer, rowHeight + (row * height), frameWidth, frameHeight)];
[self.statsView addSubview:btn.view];
And here is my class header:
#import <UIKit/UIKit.h>
#protocol OrderTypeButtonDelegate
-(void) tapped:(id)sender withOrderType:(NSString*) orderType;
#end
#interface OrderTypeButton : UIViewController {
id<OrderTypeButtonDelegate> __unsafe_unretained delegate;
IBOutlet UILabel *lblOrderType;
IBOutlet UILabel *lblOrderCount;
NSString *orderType;
NSString *orderCount;
BOOL hasOpenOrder;
}
#property (nonatomic, strong) IBOutlet UIButton *orderButton;
#property (nonatomic, strong) IBOutlet UILabel *lblOrderType;
#property (nonatomic, strong) IBOutlet UILabel *lblOrderCount;
#property (nonatomic, strong) NSString *orderType;
#property (nonatomic, strong) NSString *orderCount;
#property (nonatomic, assign) BOOL hasOpenOrder;
#property (nonatomic, unsafe_unretained) id<OrderTypeButtonDelegate> delegate;
-(id) initWithOrderType: (NSString *) anOrderType withOrderCount: (NSString *) anOrderCount hasOpenOrder: (BOOL) openOrder;
-(IBAction)btnTapped:(id)sender;
#end
Implementation:
#import "OrderTypeButton.h"
#implementation OrderTypeButton
#synthesize orderButton;
#synthesize lblOrderType, lblOrderCount, orderType, orderCount, hasOpenOrder, delegate;
-(id) initWithOrderType: (NSString *) anOrderType withOrderCount: (NSString *) anOrderCount hasOpenOrder: (BOOL) openOrder {
if ((self = [super init])) {
self.orderType = anOrderType;
self.orderCount = anOrderCount;
self.hasOpenOrder = openOrder;
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.lblOrderType.text =[NSString stringWithFormat:#"%#", self.orderType];
self.lblOrderCount.text = [NSString stringWithFormat:#"%#", self.orderCount];
if (self.hasOpenOrder) {
[self.orderButton setBackgroundImage:[UIImage imageNamed:#"background-order-btn-red.png"] forState:UIControlStateNormal];
self.lblOrderType.textColor = [UIColor whiteColor];
self.lblOrderCount.textColor = [UIColor whiteColor];
}
}
-(IBAction)btnTapped:(id)sender {
NSLog(#"TAPPED");
if ([self delegate] ) {
[delegate tapped:sender withOrderType:self.orderType];
}
}
- (void)viewDidUnload
{
[self setOrderButton:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#end
This seems fairly simple what I am doing here, not sure what changed with ARC that is causing me problems.
Maybe ARC autorelease created button, try to store created buttons in Array
//.h file
#property (nonatomic, strong) NSArray *buttonsArray
//.m file
#synthesize buttonsArray
...
- (void)viewDidLoad {
buttonsArray = [NSArray array];
...
OrderTypeButton *btn = [[OrderTypeButton alloc]initWithOrderType:#"All Orders"
withOrderCount:[NSString stringWithFormat:#"%i",[self.ordersPlacedList count]]
hasOpenOrder:NO];
btn.view.tag = 6969;
btn.delegate = self;
[btn.view setFrame:CGRectMake((col * width)+ colspacer, rowHeight + (row * height), frameWidth, frameHeight)];
[self.statsView addSubview:btn.view];
//Add button to array
[buttonsArray addObject:btn];
Also this approach will help if you want to change buttons, or remove some specific button from view