1

Newbie question for Xcode gurus...

I have two views. They both use the same custom class. In view_1 I have a button and when this is pressed view_2 will show. In view_2 I have a label which will have it´s text changed when I press the button in view_1. As of now the Label_1 is nil when I set a breakpoint at it and therefor useless. How can I get to update this label when I press the button? Her are some snippets from my code...

This is my .h file:

@interface ViewController : UIViewController
{
    IBOutlet UIButton *buttonSelectTimeInterval;
    IBOutlet UILabel *labelTimer;
}
@end

This is the button action in my .m file:

- (IBAction)startPouring_ButtonClick:(id)sender 
{
    labelTimer.text = @"foo";
}

…but my .m file doesn't seem to know the labelTimer since it is a ´nil´. Why is this so? It is instantiated in the .h file.

Anyone?

see image for overview

TommyF
  • 381
  • 1
  • 7
  • 22
  • Dude its not worked. Because when you tried to press the button from view_1 at that time view_2 properties are not assigned. One more thing you can open view_2 and on view_2 viewWillAppear method you can assign the text which you want to set. – V.J. Sep 13 '14 at 05:35

3 Answers3

1

You can use NSNotificationCenter. Put this in you IBAction.

[[NSNotificationCenter defaultCenter] postNotificationName:@"buttonPressed" object:nil];

And this to your viewDidLoad.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectorhere) name:@"buttonPressed" object:nil];

somewhere in your .m

(void)selectorhere {

labelTimer.text = @"foo";

}

Community
  • 1
  • 1
MaappeaL
  • 514
  • 1
  • 8
  • 16
  • But what is the connection to my label Label_1 that needs to be updated? And what should ´selectorhere´ be? – TommyF Sep 12 '14 at 15:55
  • Thanks a bunch! I did split my one custom class into two since I have two views, but your easy code works fine. Thanks a lot! – TommyF Sep 15 '14 at 11:16
0

You can use NSNotificationCenter for this.
Here are the apple documentation link.

Hemant Singh Rathore
  • 2,153
  • 1
  • 24
  • 38
0

First, nothing is "instantiated" in the .h file - that's just the public listing of what properties and methods are available to other classes. Think of the header file as a table of contents, but only for the things the class wants others to see.

Those properties don't exist in memory until the instance of the class itself is created, and then only if you set them to some initial value once they're needed.

How & where are you creating the 2nd view? Is it a storyboard segue or something? The 1st view doesn't seem to have any way of knowing the 2nd one exists, so it won't be able to see or access the label.

View1Class.m

#import View2Class.h 

@implementation View1Class

- (IBAction)startPouring_ButtonClick:(id)sender {
     //Instantiate the 2ndView when you need it. 
     // This gives View1 a reference to View2 and its public UILabel.  
     View2Class * my2ndView = [[View2Class alloc] init];
     my2ndView.labelTimer.text = @"foo";
}

@end

As I said, it's still not clear how/where you're actually displaying the 2nd view though, so the snippet above is incomplete. You could use a modal w/a delegate, or this is where NSNotificationCenter is a helpful option - the 2nd view can sign up to get notifications & change accordingly. There are numerous tutorials about creating a 2nd/modal view and displaying it on a button click - you should probably look at those to clarify how the structure of such an app ought to work. This answer should get you on the right track.

Other specific issues:

Why is the label nil? Because there isn't one...

In this IBAction, which seems to be in View 1:

- (IBAction)startPouring_ButtonClick:(id)sender 
{
    labelTimer.text = @"foo"; //this is looking for labelTimer in the clicked view.
}

... it is looking for its own labelTimer IBOutlet (in which case it should probably be self.labelTimer.text), and not that of the 2nd view. If the 1st view doesn't even have a UILabel IBOutlet, this is another problem.

If the views have different functions & different properties, they probably shouldn't be instances of the same custom class. If the 1st view doesn't have or need a UILabel, it shouldn't have one in its .h. If the 2nd view doesn't have or need a button it shouldn't have one in its .h. If the views serve different purposes, then make them different classes.

BTW,

Since you're using instance variables for your IBOutlets, you'd need to write your own getter & setter methods if you want to change their values. Did you? To make those values accessible to other classes, you'd need to make those methods public & put them in the .h. It's not good practice for an instance to set its instance variables directly w/o a getter/setter, and other objects definitely should not.

The preferred method is to use @properties for your IBOutlets instead of declaring them as instance variables. This will automatically create the getter & setter methods, backing store in memory, and as of XCode 4.4 it automatically adds @synthesize so you no longer need to do so. Declaring your IBOutlets as "weak" references prevents retain cycles & memory leak, where the view holds on to the outlets & the outlets hold on to the view & nothing ever goes away...

View1Class.h

@interface ViewController : UIViewController

@property (nonatomic, weak) IBOutlet UIButton *buttonSelectTimeInterval;

@end

View2Class.h

@interface ViewController : UIViewController

@property (nonatomic, weak) IBOutlet UILabel *labelTimer;

@end
Community
  • 1
  • 1
mc01
  • 3,750
  • 19
  • 24
  • Thank you very much for such a thorough answer. I have created a new class for the second view where the label is. Btw, I use storyboard and a segue. I added the NSNotificationCenter in the action for button i View 1. And added NSNotificationCenter in the viewDidLoad in View 2. And added a selector method in View 2 that updates the label. Works fine now. Thanks for all your input :) – TommyF Sep 15 '14 at 11:15
  • But… let´s say I want buttonSelectTimeInterval when I am in View2Class? Now that this button is a public property in View1Class shouldn´t it be possible to get that button now by importing View1Class into View2Class? I tried but it didn´t work. ViewController * my1View = [[ViewController alloc] init]; NSString *buttonText = my1View.buttonSelectTimeInterval.titleLabel.text; – TommyF Sep 15 '14 at 11:34