views:

192

answers:

5

Hi,

I've got a class called RootViewController with this:

Test *myLevelObject = [[Test alloc] init];
[myLevelObject afficher];

It is supposed to call my method -[ Test afficher] but there is no warning or error and it does enter the method. Any ideas? thx

Declaration of test class :

test.m :

#import "Test.h"
#import "RootViewController.h"

test.h :

#import <UIKit/UIKit.h>
@class RootViewController;
@interface Test : UIViewController <UIActionSheetDelegate,UITextFieldDelegate>{




-(void) afficher{



   NSLog(@"hello");
   poids.text = [NSString stringWithFormat:@"lol"];
   labelalco.text = [NSString stringWithFormat:@"cac"];

}

it show in console "hello" but it doesnt execute my 2 line of code.

ok now i tryed to do UIAlertView and it works but it fails only when i use the variables that are initialized in Test.h with interface builder.

Initialization :

IBOutlet UILabel *labelalco;

Interface builder : alt text

this is another screen shot of where the test and the 'drink' section which is rootviewcontroller is shown .

THANKS

+2  A: 

It's possible that your init method is passing back nil, instead of an object. If this is the case, than the -afficher method is sent to nil, which doesn't break, but doesn't DO anything. Check your return value after the alloc/init.

Ben Gottlieb
+1  A: 

Since you say an NSLog message within the afficher method is written to the console, your method is empirically being called. That is doesn't do what you intend it to do is a bug in your code. From the minimal listing you've provided, I'd guess that poids and lableco are not initialized properly (meaning they're still nil when you method is called).

Barry Wark
but when i put labelalco.text = @"dasdas"; in viewdidload it workhow to i repair this
the1nz4ne
@the1nz4ne It's impossible to debug your code without seeing it. You'll have to show us the code or we can't figure out why labelalco is not initialized in the afficher method.
Barry Wark
This is the kind of problem that debuggers were invented to solve. Set a breakpoint on the NSLog and inspect the variables. Step through the code one line at a time. That will get you an answer much quicker than asking people here to guess at the solution.
Tom Harrington
i did post more code
the1nz4ne
Yeah but I was talking about using the debugger. Did you use it? What results did you get? If you didn't use it, what are you waiting for? From what you've said it's obvious that your method is being called, so set a breakpoint already and see why those lines aren't working as expected.
Tom Harrington
A: 

If you're using IBOutlet in -init method, it won't work there (I guess it's set to nil), but it should work in -awakeFromNib method.

MLen
i am a bit noob, my labelalco is set in test.h so i need to initiale it in the test.m 'awakeFromNib' method?
the1nz4ne
Where the afficher method is called? Do you call it in -init?
MLen
-init no the afficher method is in test.h -(void)afficher;
the1nz4ne
A: 

I echo what was stated above. Your afficher is likely getting invoked if you see the "hello" output in your console. (To be absolutely certain, clear your console prior to running and check that it shows again to rule out stale output.) One other thing I would suggest is to use an NSAssert() to be sure your outlets are set. Here's an example:

-(void) afficher{

   NSLog(@"hello");
   NSAssert(nil!=poids, @"poids should be set.");
   poids.text = [NSString stringWithFormat:@"lol"];
   NSAssert(nil!= labelalco, @"labelalco should be set.");
   labelalco.text = [NSString stringWithFormat:@"cac"];

}

I just looked very closely at your screen shots and it appears that your Test object is a UIViewController. From the things you describe I bet you're assuming the labelalco to be set via interface builder connections. That will not happen in your 1st code snippet because you instantiate it directly without using the nib file.

Test *myLevelObject = [[Test alloc] init];
[myLevelObject afficher];

should be:

Test *myLevelObject = [[Test alloc] initWithNibName:@"Test.xib" bundle:nil];
[myLevelObject afficher];

Assuming Test.xib is where the connections are made in IB. It looks like this is a sub controller in your main window's tab view controller so there may be additional complexity in what you're trying to do.

Cliff
thank you , i understand more now but i did and i receive an uncaught error and it crash. 2010-02-25 09:56:17.670 DrunkeNewIdea[62171:207] hello2010-02-25 09:56:17.671 DrunkeNewIdea[62171:207] *** Assertion failure in -[Test afficher], /Users/hugohnery-garon/Desktop/DrunkeNewIdea/Classes/Test.m:342010-02-25 09:56:17.672 DrunkeNewIdea[62171:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'poids should be set.'
the1nz4ne
right, the assertion didn't pass which is just another means to find errors. Note the assertion text showing in the crash report, 'poids should be set.' The code is indeed being run and the variable is indeed nil.
Cliff
A: 

Your latest comments say you're using a UITabBarController that contains RootViewController and Test.

But in RootViewController, you're trying to call a method in Test by creating a new instance of it. This instance would be separate from the one the tabbarcontroller is using.

I think in RootViewController, you need to instead obtain a reference to the same instance of Test the tabbarcontroller is using.

See this post for a possible way to do this:

http://stackoverflow.com/questions/2083351/iphone-sdk-how-to-access-a-viewcontroller-nested-in-a-tabbar-from-myappdelegate

However, why do you have code in RootViewController that updates Test? If Test is displayed, could it not update itself instead? How does the code in RootViewController get executed after Test has already been displayed?

DyingCactus
because in rootviewcontroller i got a tableview and when there is a new item or when the user remove an item , it calls a method that show off in Test.
the1nz4ne
ok i looked the other post you send me , but i can't make a iboutlet in the appdelegate because appdelegate is in MainWindow.xib and the label is in Test.xib i can't link it.
the1nz4ne
The post is saying to create an IBOutlet for Test (the UIViewController) not the label in Test. You can also try the other way it shows which is to access the viewControllers array of the tabBarController.
DyingCactus
damn this is complicated, so i need to create a UIviewController of test in appdelegate than link it in IB to my test tabbar uiviewcontroller, than? im confused where do i initialize it and call the afficher method?
the1nz4ne
I haven't tried this but: (1) add IBOutlet for Test in MainWindow and connect it to Test. (2) in RootViewController obtain a reference to the app delegate using [[UIApplication sharedApplication] delegate]. (3) in RootViewController do Test *myLevelObject = (Test *)appDelegate.testViewController; (4) now call [myLevelObject afficher];
DyingCactus
humm says appDelete.testviewcontroller , the testviewcontroller is an error 'unknow accesor method'
the1nz4ne
In the app delegate add a property for testViewController like this: @property (nonatomic, retain) Test *testViewController; and add @synthesize testViewController in the app delegate's .m file.
DyingCactus
synthesize property testviewcontroller must either be name the same as a compatible ivar or must explicitly name an ivar
the1nz4ne
i got this error
the1nz4ne
ok good it works there is no error but it does show label.
the1nz4ne
it supose to enter aficcher method in test?
the1nz4ne
grats it finaly work , THANKSSS
the1nz4ne
Make sure name used in property and synthesize matches the name you used for the IBOutlet. Yes it should enter afficher method. What results do you see?
DyingCactus
Great, please check this as the answer. Thanks.
DyingCactus