syntax error, cant fix

Discussion in 'iOS Programming' started by lolko, Jan 28, 2010.

  1. lolko macrumors newbie

    Joined:
    Jan 28, 2010
    #1
    i have class1 and class2.
    im tryin to use Appdelegate to transfer info from class1 to class2,like that:

    http://www.iphonedevsdk.com/forum/i...ing-variables-parameters-between-classes.html



    have this problem:

    1st class:
    Code:
    #import "MyAppDelegate.h"
    
    @interface class1:UIViewController{
    
    MyAppDelegate * mainDelegate = (MyAppDelegate*)[UIApplication sharedApplication].delegate;
    
    //and other blablabla
    }
    p.s.:without it all works.


    syntax error: expected ':', ',', ';' , '}' or "_Attribute_" before '=' token


    why ?
     
  2. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #2
    You don't use braces when defining methods for '@interface' directives. They need to be in the form:

    Code:
    @interface SomeClass : SomeSuperclass {
    /* IVars go here */
    
    }
    
    /* Method prototypes go here */
    
    @end
     
  3. lolko thread starter macrumors newbie

    Joined:
    Jan 28, 2010
    #3
    ok.
    forget about that.

    1)i included MyAppDelegate.h in my class1.h file.
    next, i have a method, connected to button in class1


    2)Now, i tried to do it in 2 ways
    a)

    declaring in class1.h file
    Code:
    MyAppDelegate * mainDelegate = (MyAppDelegate*)[UIApplication sharedApplication].delegate;
    
    if i make so, i have a syntax error (u can see it at the top)



    b)
    Declaring
    Code:
    MyAppDelegate * mainDelegate;
    in class1.h file
    (no syntax errors after that)

    Code:
    mainDelegate = (MyAppDelegate*)[UIApplication sharedApplication].delegate;
    Code:
    mainDelegate.MyLabel.text = @"ASDASDASD";
    
    or
    Code:
    [mainDelegate.Mylabel setText:@"asdasd"];
    
    in my class1.m file, in the method what im using

    I have an UILabel called MyLabel in MyAppDelegate class.
    and im receiving an "error :accessing unknown "MyLabel" getter method"




    i need this to transfer some information from class1 to class2.
    I want to put some text in this MyLabel of MyAppDelegate in class1 and after that retrieve some text from this MyLabel of MyAppDelegate in class2.



    two questions : why i have syntax error in a)
    and how to access this label in delegate class, if i should chose b) way
     
  4. Troglodyte macrumors member

    Joined:
    Jul 2, 2009
    #5
    Code:
    @interface class1:UIViewController{
    
    MyAppDelegate * mainDelegate = (MyAppDelegate*)[UIApplication sharedApplication].delegate;
    
    You can't do assignments in the interface.
     
  5. lolko thread starter macrumors newbie

    Joined:
    Jan 28, 2010
  6. Troglodyte macrumors member

    Joined:
    Jul 2, 2009
    #7
    Have you defined them as properties?

    Incidentallly this is really bad design. You shouldn't be accessing UI components from all over the place. There are reasons the frameworks are designed the way they are and one is to encourage a proper MVC design. You should really put UI related stuff in a view controller not the app delegate.
     

Share This Page