Access control from one @implementation in another

Discussion in 'iOS Development' started by RazerM, Apr 28, 2008.

  1. RazerM

    RazerM New Member

    Joined:
    Oct 4, 2007
    Messages:
    279
    Likes Received:
    5
    I've created a fileButton, (A custom @implementation of a UISegmentedControl) and in it's onMouseUp function/event I want to then move that fileButton control, but can't because it is out of scope.

    Code:
    (This is kind of a mix pseudocode and Obj-C)
    
    @implementation MainView
        fileNew = [[fileButton alloc] etc];
    @end
    
    @implementation fileButton
    - onMouseUp{
        [fileNew setFrame:[10, 10, 100, 10]; //numbers dont matter
    }
    
    I'm wondering how to access the fileNew variable, I'm unsure how to make it global in objective-c because of the @interface syntax.

    I've tried putting it in UIApplcation header too.

    I'm sure there's an easy answer..
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    You might want to avoid going global with the variable and just set up a few more functions in the mainView implementation. Have one function that sets the variableand another that reads the function.

    Example, in the mainView's interface:
    Code:
    - (bool)getTheVariable;
    
    in the mainView's inplementation:
    Code:
    - (bool)getTheVariable{
        return theClassVariable;
    }
    In another class, or implementation:
    Code:
    _theClassVariable = [mainView getTheVariable];
    I think that ought to do it. Just use another similar function to set the variable. If you want mainView to generate the variable, you could do that in its init function.


    Alternately, you could try this after setting up this function in mainView:
    Code:
    - (void)setFileVariablesOne:(float) andTwo:(float) andThree:(float) andFour:(float);
    and this in the other class.
    Code:
    [mainView setFileVariablesOne: andTwo: andThree: andFour];

    If you do indeed want to go global, just #define the variable once (before you use it).
  3. RazerM

    RazerM New Member

    Joined:
    Oct 4, 2007
    Messages:
    279
    Likes Received:
    5
    Thanks skylar..
  4. RazerM

    RazerM New Member

    Joined:
    Oct 4, 2007
    Messages:
    279
    Likes Received:
    5
    In fact - I've just tried it, mainView (mainView is variable of MainView implementation) is undeclared in the fileButton implementation too.

    I'd rather use #define way you mentioned, but if i put
    Code:
    #define fileNew
    I get loads of errors everywhere I use the variable.

    Edit: It works if i just declare it outside of everything.
  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Whoops, I reread your first post again. I wasn't paying enough attention to realise that you were subclassing the UISegmentedControl.

    FileButton.h
    Code:
    #import <UIKit/UIKit.h>
    #import <UIKit/UISegmentedControl.h>
    
    @interface FileButton : UISegmentedControl
    {
    	//Add any class variables here...
    }
    - (FileButton *)initWithFrame:(CGRect)rect;
    //Add any other FileButton functions here...
    
    @end

    FileButton.m
    Code:
    #import "FileButton.h"
    
    @implementation FileButton
    
    - (MyProgressBar *)initWithFrame:(CGRect)rect {
    	self = [super initWithFrame:rect];
    	return self;
    }
    
    //Add any other FileButton functions here...
    
    
    @end
    Within your app's implementation:
    Code:
    fileNew = [FileButton initWithFrame:CGRectMake(10.0f, 10.0f, 100.0f, 10.0f)];



    **I didn't error check this code. I pulled it directly from my subclass of UIScrubberControl that I use in PocketTouch, simplified and cleaned up quite a bit, of course.
  6. TSOnTheDrums8892

    TSOnTheDrums8892 New Member

    Joined:
    Sep 26, 2007
    Messages:
    904
    Likes Received:
    7
    Device:
    2G iPod touch
    Holy Shiz....

    Frickin genious's right here on iPodtouchfans.... pretty sweet...

Share This Page