-1

In my MainClass and CustomCell class, I have two view, CustomCell is the subview of MainClass. I have a button in custom cell class like this

_(IBAction)click:sender

{
NSUInteger no =2;
second-class *sc = [[secondclass alloc] init];
[sc numberOfItem:no];
}

When I click the button I want to change the label in Main class, My main class coding is

-(void)nuberofitem:no
{
NSLog(@"number : %d",no); //Its show correct value
localtextbox.text = no;

NSLog(@"text box value : %@",localtextbox.text); //But in  label it shows null value

}

MY XIB DESIGN,

enter image description here

I have design screen like this,In run time i have screen like

enter image description here

In this have a view with name and plus and minus button,In run time i have name 1,name 2 and name 3 etc . When i click plus button increase the value from 1 to ... and show it count label,then when i click the minus button i wan decrease the count label value.I don't know how to do this thing can any know know help me please.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Yugesh
  • 4,030
  • 9
  • 57
  • 97
  • See [delegate example](http://stackoverflow.com/questions/645449/how-to-use-custom-delegates-in-objective-c). – TheTiger Apr 16 '13 at 04:28
  • localtextbox is a UITextBox, right? – DD_ Apr 16 '13 at 04:52
  • yes its a UIText Box. – Yugesh Apr 16 '13 at 04:55
  • check if the textfield is set up properly or check to see if the IBOutlet connections are made to the correct object. – DD_ Apr 16 '13 at 04:57
  • @Yugesh - I am confused at what exactly you want your application to achieve. Are you trying to have fixed cell at the top that allows you to add names (or take them away) from the table view by clicking the button in this top cell? Additionally, at the bottom of the screen you want to keep count of the names in the table view and present this value in a UITextField? – Matt Apr 20 '13 at 08:01
  • @Yugesh - Creating a CustomCell for a UITableView only allows you customise the presentation of a cell, when a cell is selected iOS calls - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath, this is the method that you would handle the action for selecting a row in a UITableView. – Matt Apr 20 '13 at 08:06
  • @Yugesh - Additionally, you must handle the information/data that fills these view cells in a model class (possible called Name). This class would be designed just to create objects that are full of the data. You would then need another Singleton Class to store the Name Objects and handle the process of adding and removing these Name Objects to and from the NSMutableArray instance in the Store. You could then update your Count UITextField text property to return the count of this NSMutableArray (which should be identical to the number of cells displayed in the UITableView). – Matt Apr 20 '13 at 08:10
  • @Matt I am used singleton class only to store name objects to and from NSMutable array what you said in last comment,but when i passing a value from custom cell class to main class get value in NSlog correctly but not display it in count label. – Yugesh Apr 20 '13 at 10:00
  • Can you email me your project? I need to see everything in your project to understand what may be going wrong. matthew.valli@gmail.com – Matt Apr 20 '13 at 10:04
  • I will look at it tomorrow when I get off of work and i'll email you my thoughts. – Matt Apr 20 '13 at 10:19

3 Answers3

2

Your code fails to update the text field in the following method:

-(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

}

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.

Here is how the computer reads your code:
1. Application is continually looping to see if the user has interacted with its interface (an event loop)
2. A button is clicked on your interface (.xib) file by the user
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 _(IBAction)click:sender)
4. The compiler locates this method in the class that you point it to with the Connections Inspector and runs the method:

a. An NSUInteger is declared and set to the value: 2
b. An instance of your second-class is allocated and instantiated (this instance is usable object of type second-class which in this case is named sc)
c. The 'sc' instance of class 'second-class' calls the method

5. We enter the method -(void) numberOfItem:(NSUInteger )no 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).
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 localtextbox.text = [NSString stringWithFormat:@"%d",no]; and converts the NSUInteger into an NSString with the 'stringWithFormat' method. This is very important else localtextbox.text will remain nil.

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.

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.

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.

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):
DISCONNECTING THE LABEL/TEXTFIELD CONNECTIONS
1. Open your .xib file for your project.
2. On the right-hand side of the screen select make sure your Xcode is set like this:
enter image description here
3. In the .xib file select the label or textField (which ever it it is).
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:
enter image description here
5. With the label/textField is selected (these connections change based on the selected interface object), Remove all connections, most importantly the 'Referencing Outlets'.

RECONNECTING THE LABEL/TEXTFIELD
1. Change your Xcode layout to the Assistant Editor Mode:
enter image description here
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).
3. In your Main.h file add a set of curly brace so that the code looks something like this:

#import <UIKit/UIKit.h>

@interface Main : UIViewController {


}

@end

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:

enter image description here

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:
enter image description here
5. Click "Connect" and your code should work assuming everything else you already said works.

Is this a subclass of UITableViewCell? If so can call [[self tableview] reloadData]; in the action method to manually update the tableview's cells.

Let me know if it still isn't working.

Matt
  • 879
  • 9
  • 29
  • its not working.See my question am include the screen shot.My exact doubt in that screen shot – Yugesh Apr 19 '13 at 05:41
1

Now that I have seen your complete project I see that you have broken some iOS conventions. Not to worry, here is a quick fix but understand that there is a more correct way of achieving this:

Add the following code to sampleViewController.m:

// At the top near the other import statements
#import 'ViewController'

-(void)viewWillAppear:(BOOL)animated {

    // Update the count in the text box when the view appears
    [self updateTableCount];

}

// Custom Method for updating the display text of the property counttxt.text
- (void) updateTableCount {

    NSLog(@"Updating Table Count");

    // Get the count of records in the name array
    int numberOfNames = [[Table_viewctrl addlist_array] count];

    counttxt.text = [NSString stringWithFormat:@"%d", numberOfNames];
}

Update the following Methods:

- (IBAction)add_namebtn:(id)sender 
{
    if(self.name_singleton != NULL)
    {
        selectname *sample_name = [[selectname alloc]init];
        sample_name.name = [nametxt text];
        [name_singleton Add:sample_name];

        nametxt.text=@"";
        [self.view endEditing:YES];

        // Update the count in the text box when the user adds a new name to the array
        [self updateTableCount];
    }
}

- (IBAction)viewlist_btn:(id)sender 
{
    self.Table_viewctrl = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    Table_viewctrl.view.frame = CGRectMake(0, 0, 320, 363);
    [self.subview addSubview:self.Table_viewctrl.view];
    [self loadnames];
    self.backbtn.hidden=FALSE;

    // Update the count in the text box when the view appears
    [self updateTableCount];
}

I will update this answer in a few days with instructions on how to properly design your application following iOS Guidelines. I'm about half way through my explanation but there is a lot to explain. Please check back after you have applied these temporary changes. There will still be a glitch, the user will not see the count updated until after the user presses the "View List" button. This is because the app does not actually move an added name to the 'add list_array until the "View List" button is selected. My next post will correct this problem as well as set up the application in the appropriate type of structure using a UINavigationBar.

Matt
  • 879
  • 9
  • 29
  • Am use your code,but when i click the delete button the row will be deleted but the count value was not reduce. – Yugesh Apr 22 '13 at 07:52
  • Yes, this is because the way you have organized your code. You could pass a reference from the sampleViewController when creating a the next view (ViewController) and then pass that reference down to your subclass of UITableViewCell which would then allow you to call the update method in sampleViewController. However, with my next post will provide a way to update your counter by calling code in only two places (when you load the view and when you return to the view). – Matt Apr 22 '13 at 11:29
  • OK Matt if get answer for my question please revert back and thanks a lot for your co-operation.me also try but i didn't get it. – Yugesh Apr 22 '13 at 13:23
0

Try this,

      -(void) numberOfItem:(NSUInteger )no
     {
      NSLog(@"number : %d",no); //Its show correct value
      localtextbox.text = [NSString stringWithFormat:@"%d",no];

      NSLog(@"text box value : %@",[NSString stringWithFormat:@"%d",localtextbox.text]); //But in  label it shows null value

   }

And check whether you have connected your IBOutlet of localtextbox properly in xib

Lochana Ragupathy
  • 4,320
  • 2
  • 25
  • 36
  • but you said your getting values in log have you connected your localtextbox IBOutlet properly? – Lochana Ragupathy Apr 16 '13 at 04:38
  • ya am connected IBOulet properly,but not able to refresh UIview from subview. – Yugesh Apr 16 '13 at 04:46
  • NSLog(@"number : %d",no); //Its show correct value are you getting your value here? – Lochana Ragupathy Apr 16 '13 at 04:48
  • localtextbox is present in the xib of the same class? – Lochana Ragupathy Apr 16 '13 at 04:58
  • 1
    just try [NSString stringWithFormat:@"%i",no] – Lochana Ragupathy Apr 16 '13 at 05:10
  • something wrong with your localtextbox plz try to figure out and use my edited code – Lochana Ragupathy Apr 16 '13 at 05:38
  • Thank, i tried your answer([NSString stringWithFormat:@"%i",no]),now i get the label value in NSLog but not change in label view. – Yugesh Apr 16 '13 at 05:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28287/discussion-between-yugesh-and-lochana-ragupathy) – Yugesh Apr 16 '13 at 06:13
  • The reason that it is not changing on the view is because 'no' is an NSInteger (a Objective C object). 'localTextbox.text' requires an NSString object. NSString stringWithFormat converts your NSUInteger into a string. At the bottom of the IBAction method try adding [self reload]; but there are better ways to setup your class to do the same thing. – Matt Apr 16 '13 at 06:16
  • @Matt but i usually set oly the text it changes perfectly without requiring a [self reload] – Lochana Ragupathy Apr 16 '13 at 06:26
  • @Matt `localtextbox.text = [NSString stringWithFormat:@"%i",count]; [self.view setNeedsDisplay]; NSLog(@"order count textbox %@",localTextbox.text);` //i get values here in NSLog but not in lable view. – Yugesh Apr 16 '13 at 06:27