Call Method From another Class

Discussion in 'iOS Programming' started by Nnavick, Jan 17, 2011.

  1. macrumors regular

    Joined:
    Oct 13, 2010
    #1
    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
    Code:
    mylable.text=@"bla bla";
    why is it skipping the above line?

    Thanks
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    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.
     
  3. macrumors 603

    Joined:
    Aug 9, 2009
    #3
    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.
     
  4. Nnavick, Jan 17, 2011
    Last edited: Jan 17, 2011

    thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #4
    I put
    Code:
    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:
    Code:
    
    #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:
    Code:
    
    @implementation TapView
    
    -(void)LableText {
            NSLog(@"ViewAppear1");
    	FlagNU++;
    	NSString * NSScore=[NSString stringWithFormat:@"%i",FlagNU];
    	flagLabel.text=NSScore;
            ViewAppear(@"ViewAppear2");
    }
    
    @end
    }
     
  5. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    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).
     
  6. thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #6
    It's spouse to be NSLog(@"ViewAppear2");,my mistake.
    How do I check that the label is not nil?
     
  7. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Off the top of my head: 1) query its value in the code, or 2) check the value in the debugger.
     
  8. thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #8
    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!!
     
  9. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    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. :)
     

Share This Page