How to bind the control on the form with variable - ios

I am just start studying iOS developing watching Stanford iOS course, but it looks like I have already missing something.
I have a form with UILabel and UIButton. When an user press the button the title of the button must be added to the text of label.
Here is my current CalculatorViewController.h:
#import <UIKit/UIKit.h>
#interface CalculatorViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *display;
#end
and here is a CalculatorViewController.m:
#import "CalculatorViewController.h"
#implementation CalculatorViewController
#synthesize display = _display;
- (IBAction)digitPressed:(UIButton *)sender {
NSString *digit = [sender currentTitle];
UILabel *myDisplay = self.display;
myDisplay.text = [myDisplay.text stringByAppendingString:digit];
}
#end
The problem is that self.display (and myDisplay) variables have a nil value. Looks like I need to do something to link my variable with control on the form. What ?
Thanks in advance.

You need to link the control, the UILabel in Interface Builder to the variable in your CalculatorViewController class.
It is very likely that the file's owner (talking about the Xib file) is your CalculatorViewController, so you need to Control+drag the file's owner (or the object representing your VC) to the control and you will be shown a menu with the possible IBOutlet variables declared in your class, so you select the one you want to represent the control.
You can check if this link is properly set in two ways: (1) Select the UILabel in Interface Builder and see if there's a connection to the variable in the Connections Inspector, or (2) In your code you'll see a bullet near the IBOutlet declaration, if the bullet is hollow the connections is not set, if the bullet is filled the connection is set.

There is no need of this line
UILabel *myDisplay = self.display;
You have already declared your label in your interface file
- (IBAction)digitPressed:(UIButton *)sender
{
NSMutableString *string = [myDisplay.text stringByAppendingString:sender.titleLabel.text];
myDisplay.text = string;
}

Related

Having trouble passing contents of UITextField from one controller to another

I want to pass whatever string a user inputs into a UITextField' over to the destination view controller, but when I NSLog the value in the destination, it shows up as null. I'm also getting a warning stating:incompatible pointer types assigning to UITextField from NSString`. How can I properly make the assignment and send it over?
Originating view controller
SearchViewController.m:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
controller.itemSearch = self.itemSearch.text;
}
SearchViewController.h:
#property (weak, nonatomic) IBOutlet UITextField *itemSearch;
Destination view controller
UserCategoryChooserViewController.h:
#import <UIKit/UIKit.h>
#import "SearchViewController.h"
#interface UserCategoryChooserViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *itemSearch;
UserCategoryChooserViewController.m:
NSLog(#"The text is:'%#'", self.itemSearch.text); // results in (null)
The above two answer are also correct. But if you want to make the assignment properly then modified textfield variable as NSString variable like that below:
#interface UserCategoryChooserViewController:UIViewController
#property (strong, nonatomic) NSString *itemSearch;
After doing this your warning
incompatible pointer types assigning to UITextField from NSString will get removed
controller.itemSearch is a UITextField *, and you tried to set it to a NSString *, that's what that warning is for. To set text of a UITextField, try this:
[controller.itemSearch setText:self.itemSearch.text];
There might be other issues, but let's start from here.
Let's go through the error you are seeing. It says that you are assigning an NSString * to a UITextField *. Unfortunately, you cannot do that. Here, itemSearch is the text field.
To make your code run properly, you need to set the text property of the itemSearch text field. The correct code would be:
controller.itemSearch.text = self.itemSearch.text;
As a side note, you should really not be meddling so deep into iOS development until you have made a strong foundation, which you have not.

Simple math app terminated in Xcode- Why?

Alright, so I'm trying to do something very, very simple: Multiplying the user input from one textfield by another textfield and feeding out the answer through a label. There are no error messages, but when I try to run the app, the simulator screen goes black and Xcode says this:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key outputlabel.'
I have no idea why it is doing this. Any suggestions? Please be extremely specific as I am new to programming.
My code:
//
// ViewController.h
// Cramer
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
// Objects are given addresses:
#property (weak, nonatomic) IBOutlet UITextField *box_a;
#property (weak, nonatomic) IBOutlet UITextField *box_b;
#property (weak, nonatomic) IBOutlet UILabel *hiLabel;
#property (weak, nonatomic) IBOutlet UIButton *clickButton;
#end
AND
//
// ViewController.m
// Cramer
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (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.
}
// Math takes place:
- (IBAction)clickButton:(id)sender {
NSInteger number1 = [self.box_a.text integerValue];
NSInteger number2 = [self.box_b.text integerValue];
NSInteger prod = number1 * number2;
self.hiLabel.text = [NSString stringWithFormat:#"%#", #(prod)];
}
#end
It seems like in your view controller XIB file, created using the Interface Builder or Storyboard graphic editor, you have some view (probably a UILabel) connected to a "outputlabel" property of the File Owner (that is your ViewController class) which doesn't exist.
Typically this happens when you initially connect an object from the XIB to a new outlet in the code, then you rename the outlet in the code but you forget to reconnect the object in the XIB to the new reanmed outlet. It could be, in your case, that you initially connected the output label to a IBOutlet property called "outputlabel", then you renamed it to "hiLabel" but you forgot to reconnect the label in the XIB to the new "hiLabel" IBOutlet.
To verify this you can look at all your objects in the view controller graphical interface, right-click on each of them and see their outlet connections and check that they point to properties effectively defined in the class.
your label on your storyboard is hooked up to a UILabel in your view controller named "outputlabel". However in your view controller you have it named "hiLabel". If, in interface builder, you click on your label then go to view -> utilities -> connection inspector you'll see the connections on the right hand side and should be able to verify what it's connected to. Either delete the connection and start over or change the name of your label in your view controller.
In the last line [NSString stringWithFormat:#"%#", #(prod)];, why are you using %#? NSInteger is a primitive type, not an object. It seems to me you should use a %d so that the line would read [NSString stringWithFormat:#"%d", prod];.
ETA: Use %ld instead of %d.

How to set label text to a variable as the view loads?

So I have this little project in which the user goes from one view to another as they click buttons, and the text on the last button they've clicked should show up in a label in the top of the screen. I figured out how to have the variables accessible from anywhere in the app, and also how to change it as the user presses the buttons. Yet, I can only make the label's text change once a button is hit, and I wanted it to happen automatically (the view to appear with the label already containing the correct text).
For that I set the label in two different ways (which I found online):
#interface ViewMateria : UIViewController
{
IBOutlet UILabel *materiaLabel;
}
#end
and
#interface ViewMateria : UIViewController
#property (weak, nonatomic) IBOutput UILabel *materiaLabel;
#end
Still, for both of this I can not access the label by a simple materiaLabel.text or anything like that, and can't find a way to do it without the need for the press of a button.
I've been looking for an answer for this for a while now, and nothing that I found seemed to be of use for me (since most of the links I found taught how to change a label text with the press of a button).
This is my first project with Objective-C and Xcode, and I don't really know the answer, so maybe I am missing something quite obvious, but still, if anyone could point me in the right direction, that would be of much help.
Put this in your .h file for the view controller:
#property (weak, nonatomic) IBOutput UILabel *materiaLabel;
When you are about to present the view controller, access it like this:
your_ViewMateria_object.materiaLabel.text = #"";
Then present it..
Are you using segues?
If yes, its another story..
UPDATE:
For segues use this, with the .h property value I just mentioned:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
ViewMateria *vm = [segue destinationViewController];
vm.materiaLabel.text = #"Whatever";
}
You should be good to go now ;D
Did it work?
UPDATE
In second view controller .h file
#property (strong, nonatomic) NSString *buttonName;
.m file
self.materiaLabel.text = self.buttonName;
In first view controller .m file
inside IBAction
secondviewcontrollerObject.buttonName = [(UIButton *)sender currentTitle];

How can I manipulate objects in a subView?

Got away from XCode programming for a while, now I'm starting back and feel like I'm starting ALL over. I have a simple example that's driving me crazy.
I have created a sub class of UIView called Word1View and added it to my storyboard.
I declared it as a property in the main view controller and attached it as an IBOutlet to the main view. I can draw on this view using Core Graphics with no problems (rectangles, lines, etc). Here's the code in the ".h" file. The property is synthesized correctly in the ".m"
#import <UIKit/UIKit.h>
#import "Word1View.h"
#interface Board3ViewController : UIViewController
{
Word1View *word1View;
}
#property (nonatomic, strong) IBOutlet Word1View *word1View;
#end
Now, here's the problem. I have added a UILabel (I will be adding UIImageFiles later)
in Word1View.h it appears as so:
#import <UIKit/UIKit.h>
#interface Word1View : UIView
{
UILabel *testLabel;
}
#property (nonatomic, retain) UILabel *testLabel;
#end
This property is also synthesized correctly. The UILabel was added to the view in the main storyboard. I tried to make this an IBOutlet, but could not hook it up as long as it was part of the subview. I tried to access the UILabel in my main view controller as so:
[super viewDidLoad];
[self.view addSubview:word1View];
NSLog(#"Label text is: %#", word1View.testLabel.text);
I don't have any access to that label as evidenced by the output to NSLog which is:
Board3[14520:c07] Label text is: (null)
I would like to be able to access the label (change its text, properties etc) from my main View Controller.
Any help is greatly appreciated. Thank you.
You need to instantiate your UILabel and give it some text.
wordView1 = [[UILabel alloc] init];
wordView.text = #"Some Text";

How to change a label on one UIView from another UIView

Within my iPhone app I have 2 UIControls a ViewController.h and a selectionScreen.h.
The selectionScreen is set as so
#interface selectionScreen : ViewController{
I am trying to change a label that is placed in the main ViewController, from the selectionScreen
So far I have this within the SelectionScreen.m (personTotal1.text is the label in the other UIcontrol)
- (IBAction)Change:(id)sender{
int x=123;
NSString *y =[NSString stringWithFormat:#"%i",x];
personTotal1.text=y;
NSLog(#"%#",personTotal1.text);
}
When I NSLog to check if the value has changed it returns null, how can I make it so interactions(such as a button press) within the Selectionscreen change label text in another screen.
In your View controller:
Did you set your personTotal1 ivar as a property? Just setting the ivar is not enough:
UILabel *personTotal1;
You need to set the property and synthesize the getters and setters like so:
#property(nonatomic, retain) UILabel *personTotal1;
If you are using ARC then it would be like so:
#property(nonatomic, strong) UILabel *personTotal1;
Then be sure to synthesize the label. Now import the ViewController.h into the SelectionScreen file. From there you can access the UILabel properties. Hope this helps, if not let me know and I can clarify.

Resources