开发者

How can I execute code from another methods property

Sorry if the question is not to clear, this is what im trying to do. I have an object, a delegate and a view controller.

Object pseudo-code

@interface Car: NSObject {
    UIColor *color;
}
@property (assign)UIColor *color;
- (void) setColor(UIColor col);
@end

@implementation Car
@synthetize color;
// i know that synthesize makes this function redundant. 
// I just used it to demonstrate
// the need to access an instance method.
- (void) setColor(UIColor col)
{
    color = col;
}
@end

delegate code

@interf开发者_如何学运维ace myDelegate: UIApplicationDelegate {
    Car *car;
    UIViewController *theView;
}
@property (assign)Car *car;
@end

@implementation myDelegate
@synthesize car;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{    
    theView = [[MyViewController alloc]init];   

    return YES;
}

@end

View pseudo Code

@interface MyViewController: UIViewController {

    MyDelegate *appDelegate;
}
@property (retain) MyDelegate *appDelegate;
@end

@implementation MyViewController
@synthesize appDelegate;

- (void)viewDidLoad {
    self.appDelegate = (MyDelegate*)[[UIApplication sharedApplication] delegate];
    /// THIS IS MY ISSUSE!
    [self.appDelegate.car setColor(UIColor);
}
@end

Can anyone explain or point me to where i can understand why [self.appDelegate.car setColor()] gives me a compile error that reads "Unknown component setColor of a property".

Im sure there is a way to do this in objective C as i would do it in python, java or other OO language.

Any help would be greatly appreciated

Cheers Rudy


You are not using UIColor as a pointer.

Try using UIColor * instead of just UIColor and the compiler will stop complaining


First of all, the Car class has problems. The color property should be defined as retain, not assign. assign is generally for non-object type properties, such as an NSInteger, BOOL, int, etc., and for a special case of objects that shouldn’t be retained because they’d create retain cycles. Also, - (void)setColor(UIColor col); is not a valid method name, as it is written as if it were a function. In Objective-C, each parameter is preceded by a colon :.

For example, take the following method:

 - (void)setBodyColor:bodyColor lowerColor:lowerColor upperColor:upperColor;

While that is technically a valid method signature, it is generally written differently to make its usage more clear. As it’s defined above, each parameter is of type id, which is a generic object type. To make things clearer, you cast each argument to the type of objects they represent:

 - (void)setBodyColor:(UIColor *)bodyColor
           lowerColor:(UIColor *)lowerColor
           upperColor:(UIColor *)upperColor;

In addition to being defined incorrectly, it’s also superfluous since defining a read-write property named color implies that a -setColor: method will be defined. The code would look like this:

@interface Car: NSObject {
    UIColor *color;
}
@property (retain) UIColor *color;
@end


@implementation Car

@synthetize color;

- (void)dealloc {
   [color release];
   [super dealloc];
}
// If you need to override a method, that’s fine
- (void) setColor:(UIColor *)aColor
{
    [aColor retain];
    [color release];
    color = aColor;
    // do more stuff
}
@end

On to your delegate, it also has problems. First, myDelegate is defined as a subclass of UIApplicationDelegate, which is not even a class: it’s a protocol (or interface) that other objects can conform to. The car property should also be defined as retain, since it’s an object that your app delegate owns. Here, theView (which should likely be renamed to something like theViewController) should be typed as MyViewController to make it more clear.

@interface MyDelegate : NSObject <UIApplicationDelegate> {
    Car *car;
    MyViewController *theView;
}
@property (retain) Car *car;
@end


@implementation MyDelegate

@synthesize car;

- (void)dealloc {
   [car release];
   [theView release];
   [super dealloc];
}

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{    
    theView = [[MyViewController alloc] init];   

    return YES;
}

@end

The MyViewController class has problems in that the appDelegate is defined as retain when it should likely be assign. The app delegate itself is creating the view controller using alloc/init, meaning the app delegate “owns” the view controller. The view controller shouldn’t retain the app delegate because that would create a retain cycle (see Retain Cycles).

MyViewController.h

// forward declaration
@class MyDelegate;

@interface MyViewController: UIViewController {
    MyDelegate *appDelegate;  // non-retained
}
@property (assign) MyDelegate *appDelegate;
@end

MyViewController.m

#import "MyViewController.h"
#import "MyDelegate.h"

@implementation MyViewController

@synthesize appDelegate;

- (void)viewDidLoad {
    self.appDelegate = (MyDelegate*)[[UIApplication sharedApplication] delegate];
    [self.appDelegate.car setColor:[UIColor clearColor]];
    /// THIS IS MY ISSUSE!
    // [self.appDelegate.car setColor(UIColor);
}
@end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜