开发者

How to call methods in objective-c?

I am actually following a tutorial to create a custom view.But when i try to call methods it doesnot work

Here is the tutorial http://dialogtree.com/2011/02/ios-development-a-shape-class/

So 1)I have created a new iPhone project Test.xcodeproj

开发者_如何转开发

2)Include both the classes form tutorial Shape.h and Shape.m

3)Now i heve imported Shape.h to my TestViewController.m where i am going to display this.

i am trying to call methods in my TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];

        Shape * shape;
    shape = [Shape alloc];

    shape = [shape setLocationWithX:10 andY:20];

    [shape addPointWithX:10 andY:10];

    [shape setColorWithR:20 andG:30 andB:40];
    [shape setAlpha:0.5];
}

But it does not showing anythig, why?

What i am going wrong?


For starters the correct instantiation for an object is:

shape = [[Shape alloc] init];

Then the line

shape = [rectangle setLocationWithX:10 andY:20];

overwrites the shape object just created.


After reading the definition and implementation of the Shape class I see there is a draw method. But this -draw method of your Shape class won't call itself!

You need to call [shape draw:UIGraphicsGetCurrentContext()] in the implementation of the -(void)drawRect:(CGRect)rect method of your custom view to actually ask the shape to draw itself!


[EDIT] The answer of CocoaFu is also true and is another point you need to fix in your code

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜