PDA

View Full Version : Call Method From another Class




Nnavick
Jan 17, 2011, 04:22 PM
Hi,
I have 2 .NIB files and each of them have 2 .h and .m files
I called a function I wrote on the first .m file from the second .m file,
the method saying that every time the function get called,the lable.text is
bla bla.
but when I'm calling the function it's skipping the line that says mylable.text=@"bla bla";

why is it skipping the above line?

Thanks



dejo
Jan 17, 2011, 04:42 PM
It's probably not skipping that line (unless, of course, you have it inside some kind of conditional logic). You're probably just not getting the intended effect. So, first, have you tried setting a breakpoint near that line to confirm that is is being skipped?

P.S. It will probably help if you included the code for the entire method from the first class that you are calling from the second class, as well as the .h for the first class. Then you should also include the code from the second class that calls this method from the first class, as well as the code you use to instantiate an object of the first class from within the second. Please be a specific as possible. Then we can avoid using phrasing like "first class" and instead use something like FirstClassName.

chown33
Jan 17, 2011, 05:23 PM
Hi,
but when I'm calling the function it's skipping the line that says mylable.text=@"bla bla";

why is it skipping the above line?


How do you know it's skipping the line? Are you basing this conclusion on the observation that the label's text doesn't change? If so, then such an observation could also be explained by either of the following:
A. mylable is nil.
B. mylable isn't displayed.


Always provide the following:
1. Describe what you expected to happen.
2. Describe what actually happened.

You have described neither what you expected, nor what actually happened. What you have done is give your conclusion based on something you did or saw, but you haven't described what you did or saw.

Nnavick
Jan 17, 2011, 11:03 PM
How do you know it's skipping the line? Are you basing this conclusion on the observation that the label's text doesn't change? If so, then such an observation could also be explained by either of the following:
A. mylable is nil.
B. mylable isn't displayed.


Always provide the following:
1. Describe what you expected to happen.
2. Describe what actually happened.

You have described neither what you expected, nor what actually happened. What you have done is give your conclusion based on something you did or saw, but you haven't described what you did or saw.

I put NSLog(@"ViewAppear"); below the method title and below the line that the method is skipping and on the debugger console screen it wrote "ViewAppear" twice.

The FirstViewClass:


#import "TapView.h"
@implementation Select

- (void)viewDidLoad {
[super viewDidLoad];
TapMet = [[TapView alloc] init]; // I wrote "TapView *TapMet" at the .h file
}

-(void)viewWillDisappear:(BOOL)animated {
[TapMet LableText];

@end
}

The SecondViewClass:


@implementation TapView

-(void)LableText {
NSLog(@"ViewAppear1");
FlagNU++;
NSString * NSScore=[NSString stringWithFormat:@"%i",FlagNU];
flagLabel.text=NSScore;
ViewAppear(@"ViewAppear2");
}

@end
}

dejo
Jan 18, 2011, 09:41 AM
First, what other debugging have you done within the LableText method? Have you checked that FlagNU is being incremented correctly? Have you checked that NSScore is being set as hoped? Have you confirmed that flagLabel is not nil? Etc.

Second, what the heck is ViewAppear(@"ViewAppear2"); ? Does that even compile? Seems you might not even be copying the actual code from your project to show to us. Please do.

Third, normal coding convention is that variables and methods begin with lower-case and class names begin with upper-case. Mixing the two can cause confusion to readers of your code. So, I would suggest: TapMet => tapMet, LableText => lableText, FlagNU => flagNU, NSScore => score (the NS prefix is unnecessary and makes one think it's referencing a Foundation class; it's not).

Nnavick
Jan 18, 2011, 11:09 AM
First, what other debugging have you done within the LableText method? Have you checked that FlagNU is being incremented correctly? Have you checked that NSScore is being set as hoped? Have you confirmed that flagLabel is not nil? Etc.

Second, what the heck is ViewAppear(@"ViewAppear2"); ? Does that even compile? Seems you might not even be copying the actual code from your project to show to us. Please do.

Third, normal coding convention is that variables and methods begin with lower-case and class names begin with upper-case. Mixing the two can cause confusion to readers of your code. So, I would suggest: TapMet => tapMet, LableText => lableText, FlagNU => flagNU, NSScore => score (the NS prefix is unnecessary and makes one think it's referencing a Foundation class; it's not).

It's spouse to be NSLog(@"ViewAppear2");,my mistake.
How do I check that the label is not nil?

dejo
Jan 18, 2011, 11:33 AM
How do I check that the label is not nil?
Off the top of my head: 1) query its value in the code, or 2) check the value in the debugger.

Nnavick
Jan 18, 2011, 01:45 PM
Problem solved by my way :D:D:D:D

I linked the UIImageView from the 1st view to the 2nd view and it solved everything

Thanks a lot!!

dejo
Jan 18, 2011, 01:58 PM
You should've taken this as an opportunity to learn some debugging. If you want to avoid that now, well, I guess we'll see you later. :)