开发者

How to access a instance method of a custom class in ObjC (iOS programming)

I'm playing around with the iOS SDK and tried to use a selfmade class within an iPhone app.

#import <Foundation/Foundation.h>
#import "UIKit/UIKit.h"

@interface MyPoint
: NSObject {
   CGFloat x;
   CGFloat y;
}
- (void) setX:(CGFloat)x_;
- (void) setY:(CGFloat)y_;
- (CGFloat) getX;
- (CGFloat) getY;
- (CGFloat) getDistance;

@end

This is the header of my Point class. Now in the app I want to enter 2 numbers (in two different text fields) and then when I press the button "Compute" I call the function getDistance

- (IBAction)calcDistance:(id)sender {
  CGFloat x = [fieldx.text doubleValue];
  CGFloat y = [fieldy.text doubleValue];
  [point setX:(x)];
  [point setY:(y)];
  CGFloat test = [point getX];
  CGFloat test2 = [point getY];
  CGFloat result = [point getDistance];
  NSString *resultString = [[NSString alloc] initWithFormat: @"Distance: %f",result];    
  resultLabel.text = resultString;
  [resultString release];
}

I was under the impression that

[name method]

is the way to access member methods of a pointer, but that doesnt do anything, when debugging any of the values that I want to assig开发者_如何学Gon in the above manner, nothing changes.

Lastly, the interface of my app:

@interface iOS4ViewController : UIViewController {
   UITextField*    fieldx;
   UITextField*    fieldy;
   UILabel*        resultLabel;
   MyPoint* point;
}

Hopefully I was clear enough that someone can point out the obvious mistake I'm doing :)

Thanks!


It looks to me that you may not have allocated an actual instance of MyPoint. Does it work if you try it like this:

- (IBAction)calcDistance:(id)sender {
    point = [[MyPoint alloc] init];
    CGFloat x = [fieldx.text doubleValue];
    CGFloat y = [fieldy.text doubleValue];
    [point setX:(x)];
    [point setY:(y)];
    CGFloat test = [point getX];
    CGFloat test2 = [point getY];
    CGFloat result = [point getDistance];
    NSString *resultString = [[NSString alloc] initWithFormat: @"Distance: %f",result];    
    resultLabel.text = resultString;
    [resultString release];
    [point release];
}

As for how to call a method on an instance of a custom class (or any other Object in Objective-C), you are correct, the proper syntax is [myObjectVarName method].


Did you alloc point? If an object is nil, calling a method on it will return nil, not an error.


I assume that you haven't defined those getter/setter methods declared in the header files. If that is the case, you have to either define them in the implementation file, or, you can use declared properties, to achieve what you need. Create properties in header files like this,

@property(nonatomic) CGFloat x, y;

Synthesize it in the implementation file,

@synthesize x, y;

Synthesizing properties automatically creates the getter and setter method for the properties.


you also need to include the Point.h file in which you are trying to call methods...


First off: thanks for all the help. It works now that I've called

 point = [[MyPoint alloc] init];

within the function calcDistance as aroth (and others) suggested.

@EmptyStack, I had the setter and getter defined in the implementation file but thanks for the hint about declared properties.

Cheers!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜