Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your code fails to update the text field in the following method:</p> <pre><code>-(void)nuberofitem:no { NSLog(@"number : %d",no); //Its show correct value localtextbox.text = no; // THIS IS WHERE YOU WENT WRONG NSLog(@"text box value : %@",localtextbox.text); //But in label it shows null value } </code></pre> <p>localtextbox.text is a property of UITextField or Label that requires a NSString Object to be passed to it. You are passing a NSUInteger Object, which will just be read as 'nil' by the compile since it is an object of the wrong type. To better understand what's happening you have to read your code like the compiler does.</p> <p>Here is how the computer reads your code:<br /> 1. Application is continually looping to see if the user has interacted with its interface (an event loop)<br /> 2. A button is clicked on your interface (.xib) file by the user<br /> 3. The event loop pauses to notice that a button has been pressed. It looks at the xml information hidden from view to us as developers in Xcode and looks at its 'target' property. This target property holds a pointer to a 'selector' (which is just a fancy word for your IBAction method <code>_(IBAction)click:sender</code>)<br /> 4. The compiler locates this method in the class that you point it to with the Connections Inspector and runs the method:<br /><br /> a. An NSUInteger is declared and set to the value: 2<br /> b. An instance of your <code>second-class</code> is allocated and instantiated (this instance is usable object of type <code>second-class</code> which in this case is named <code>sc</code>)<br /> c. The 'sc' instance of class 'second-class' calls the method<br /><br /> 5. We enter the method <code>-(void) numberOfItem:(NSUInteger )no</code> in which we pass our variable 'no' which contains the value: 2 (in actuality it is a reference to a piece memory, but you can think of it as the literal value 2).<br /> 6. The compiler hits the first NSLog statement and replace the '%d' formatter with the NSUInteger value (this is how this particular formatter is designed to work). 7. The compiler reaches <code>localtextbox.text = [NSString stringWithFormat:@"%d",no];</code> and converts the NSUInteger into an NSString with the 'stringWithFormat' method. This is very important else localtextbox.text will remain nil.</p> <p>So far everything is perfect, next Cocoa and Cocoa Touch we do some work for us if we built our interface using interface builder properly.</p> <p>Xcode has built in mechanisms that do a lot of the work for us as long as we follow the rules of the development environment. Labels will automatically update if their 'text' property is change and this change will happen immediate, but only if we have declared the textField or label as an IBOutlet (we can do this in code or using Interface Builder and the Connections Inspector). Set a label to be an IBOutlet is like telling the textField or label, "Hey label, if your 'text' property is changed, then update your view to reflect that change." You have explicitly tell it to watch for a change or the label thinks that the change is unimportant in regards to the what is displayed to the user.</p> <p>If you have properly set up the label as an IBOutlet then your code will automatically update the view when the localtextbox.text property was changed and would continue to the next line and properly logged the value of the localtextbox.text property as it does anyways.</p> <p>You need to revisit your Connections in the Connections Inspector. Follow these steps (it can be easy when you are starting out to accidentally make the wrong connect):<br /> DISCONNECTING THE LABEL/TEXTFIELD CONNECTIONS<br /> 1. Open your .xib file for your project.<br /> 2. On the right-hand side of the screen select make sure your Xcode is set like this:<br /> <img src="https://i.stack.imgur.com/YOpbb.png" alt="enter image description here"><br /> 3. In the .xib file select the label or textField (which ever it it is).<br /> 4. While the label/textField is selected, navigate to the right side of the screen and select the right most option in the upper toolbar in the Identity Inspector Panel (you can hover for with your mouse over the tools and a tool tip should appear that says 'Connections Inspector' which is a circle with an arrow inside pointing right:<br /> <img src="https://i.stack.imgur.com/ym8PB.png" alt="enter image description here"><br /> 5. With the label/textField is selected (these connections change based on the selected interface object), Remove all connections, most importantly the 'Referencing Outlets'.<br /></p> <p>RECONNECTING THE LABEL/TEXTFIELD<br /> 1. Change your Xcode layout to the Assistant Editor Mode:<br /> <img src="https://i.stack.imgur.com/2c1TZ.png" alt="enter image description here"><br /> 2. As long as your .xib file was open when you did this, the Main.h file should also open (this is what we want).<br /> 3. In your Main.h file add a set of curly brace so that the code looks something like this:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface Main : UIViewController { } @end </code></pre> <p>4. With your mouse over the label, control-drag from the label/textField to the area between these curly braces and release (you should see a blue line when doing this). Like so:<br /></p> <p><img src="https://i.stack.imgur.com/ulzVZ.png" alt="enter image description here"></p> <p>Like the picture reveals, a message should popup stating, "Create Outlet or Outlet Collection". We want to create an Outlet (IBOutlet). When you release a form will popup. We want to select connection: 'Outlet', name it the name of our textfield:localtextfield and make sure that the reference is 'Weak'. Like so:<br /> <img src="https://i.stack.imgur.com/CkIin.png" alt="enter image description here"><br /> 5. Click "Connect" and your code should work assuming everything else you already said works.</p> <p>Is this a subclass of UITableViewCell? If so can call <code>[[self tableview] reloadData];</code> in the action method to manually update the tableview's cells.</p> <p>Let me know if it still isn't working.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload