1

How to update a label by button clicks when they both are in different classes of uiview controller...when button is clicked,the label should be update...i tried many times..but its not happening..

one more Question is my app is running good in simulator but when i run on device the dynamically created button(button image) is not visible,action is performing but image is missing..may i know why?

Cgs
  • 13
  • 1
  • 6
  • one more prob is that when app is running the score,correct images are ticked with correct png file n wrong with wrong png file,but when app is closed and started again the score is refreshed....if we go back and forward the score and images are ok..but when app is closed,the score is 0... – Cgs Jun 29 '12 at 12:39
  • How to lock and unlock the levels of a game through coding.... i used level.enabled=NO; my first level of a game app is open and other levels are locked..so i kept a condition and once condition is satisfied,it unlocks the level..but my prob is that when i press back button then again its unlock the next level which are opened... can anyone help me in it..... ThankQ in adv... – Cgs Jul 06 '12 at 12:34

2 Answers2

7

There are a few ways to maintain communication between views (view controllers, actually) in iOS. Easiest of which for me is sending notifications. You add an observer for a notification in the view you want to make the change, and from the view that will trigger the change, you post the notification. This way you tell from ViewController B to ViewController A that "something is ready, make the change"

This, of course, requires your receiver view to be created and already be listening for the notification.

In ViewController B (sender)

- (void)yourButtonAction:(id)sender
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"theChange" object:nil];
}

In ViewController A (receiver) Add the observer to listen for the notification:

- (void)viewDidLoad
{
    //.........
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(makeTheChange) name:@"theChange" object:nil];
}

Do NOT forget to remove it (in this case, on dealloc)

- (void)dealloc
{
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"theChange" object:nil];
     [super dealloc];
}

And finally, the method that will update your label

- (void)makeTheChange
{
    yourLabel.text = @"your new text";
}
Eren Beşel
  • 1,047
  • 8
  • 16
  • Where does it fail? Which method does not get called? Some specifics would be great. – Eren Beşel Jun 29 '12 at 10:22
  • i just added ur methods as it is in my app...and it runned successfully,but when i gave the correct answer for my image,and went back to check whether it is changed or not..but it doesnt change....i dint kept breakpoint and check as at which point it stopped... – Cgs Jun 29 '12 at 12:18
  • even i tried as label1.text=[NSString stringWithFormat:@"%@",15-hits];same for label 2,3 but its not showing...in nslog it displays as 14hits are required,13hits are required....but when i go back and check again its showing 15hits are required..and checked the labels text as label1.text and it displays null.. – Cgs Jun 29 '12 at 12:25
  • Null means you did not initialize your labels. If you are adding them programmatically, don't forget to use `[[UILabel alloc] init]...` and `[view addSubview:yourLabel]`. Or if you are adding them on a xib file, then you must have forgotten to properly link them to your `IBOutlet` label. Control-drag to the label in xib file and choose the name of your label on the small window that appears. – Eren Beşel Jun 29 '12 at 12:38
  • i created label n button through xib and gave proper connection...do i need to give the connection to makeTheChange method...but as its not uiaction so its not displaying in xib file...what to do now... – Cgs Jun 29 '12 at 12:51
  • Try `label1.text=@"some text";` if even this can't change the text of `label1` then `label1` is definitely not initialized. Make sure it's connected in your xib file to your .m file. If it does, then the weird named property `15-hits` is null. Check its value. And please, post some code and that would really speed things up. – Eren Beşel Jun 29 '12 at 12:58
  • label displays the text in it...15-hints is not property...its like minus hits from 15 i.e., 15-hits.. if ((hits >=0) || (hits<=130)) { label1.text=[NSString stringWithFormat:@" %d hints are required to unlock the next level",15-hits]; NSLog(@"%d hits are required to unlock next level in nslog",15-hits); } – Cgs Jun 29 '12 at 13:06
  • My bad, got confused when i saw `@"%@"` instead of `@"%d"`. But again, i really don't think i can say anymore without seeing some of your code. Take the time to post some code by editing your question if you don't mind. – Eren Beşel Jun 29 '12 at 13:17
  • Glad it helped. Not sure if you'll see but here at SO, you should make it a habit to accept answers if they solved your problem. Cheers. – Eren Beşel Jul 02 '12 at 06:53
  • ThankQ...and i visit the stack overflow regularly...and Hatsoff to U.. :-) – Cgs Jul 02 '12 at 08:49
  • ohh that i dont knw...any now i did..one more Question i want to ask...how can i use plist and update my hints.as bydefault there will b 3 hints globally..but when user hits correct 5 answers or it reaches 1000,2000 then hints will b increased..can u help me in it.. – Cgs Jul 02 '12 at 10:30
  • [bankScoreArray replaceObjectAtIndex:[bankScoreArray count]-1 withObject:[NSString stringWithFormat:@"%d",hintCount]]; if ((personScore == 1000*n) || (hits == 5*n)) { hintCount++; personScore=personScore+0; n++; hintLabel.text=[NSString stringWithFormat:@"%i", hintCount]; } once im going back and playing again then the score and hintscount is showing somethng different values... – Cgs Jul 02 '12 at 10:34
  • Firstly, you accepted the wrong answer :) secondly, you need to ask a different question about it though i may not be very helpful about it. But one post should be for one topic only. – Eren Beşel Jul 02 '12 at 10:37
0

Not sure if it's a good solution, but you could store the text in a global NSString when clicking on the button, then put that string into your label when loading the second view.

Cake
  • 109
  • 1
  • 9
  • as i used plist for score,images and hints...so do i need to store all that text of labels in db? – Cgs Jun 29 '12 at 09:48