Subscribe ( )

iPhone Programming Tutorials

 

Forum

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

You must be logged in to post Login Register

Search 

Help with view controllers and views

User Post

12:40 pm
February 7, 2009


hk

Noob

posts 2

1

I am new to objC and MVC.  Need help with some basic stuff.

I am trying to create a redball programmatically using the view controller and UIImageView.  My view controller is also created programmatically and so is the view.  The ball does not appear (let alone animate it later) however I try on the simulator.  Any help appreciated.  What am i doing wrong?  Here is my code snippet … 

my app delegate.h:

@class HKViewController;


@interface HKAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;

    HKViewController *viewController;

}


@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) HKViewController *viewController;

—————————-
my appdelegate.m: (not showing the dealloc methods)

@implementation HKAppDelegate

@synthesize window;

@synthesize viewController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

viewController = [HKViewController alloc];

self.viewController = viewController;

[window addSubview:viewController.view];  // is this required?

[window makeKeyAndVisible];

}

———————-
my view controller without nib file i.e. programmatically drawing the ball (but it doesn't show up :(in the first place …. let alone animate it later … what am i doing wrong
my view controller.h:

#import <UIKit/UIKit.h>

@interface HKViewController : UIViewController {

CGPoint pos;

UIImageView *redBallView;

}

@property(nonatomic,retain) UIImageView *redBallView;

@end

my view controller.m: (not showing memory warning and dealloc methods)

#import “HKViewController.h”

@implementation HKViewController

@synthesize redBallView;

-(void) onTimer {

pos = CGPointMake(160,300);

redBallView.center = CGPointMake(redBallView.center.x+pos.x,redBallView.center.y+pos.y);

if(redBallView.center.x > 320 || redBallView.center.x < 0)

pos.x = -pos.x;

if(redBallView.center.y > 320 || redBallView.center.y < 0)

pos.y = -pos.y;

}


// Implement loadView to create a view hierarchy programmatically, without using a nib.

- (void)loadView {

// Drawing code

UIGraphicsBeginImageContext(CGSizeMake(320,420));

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextBeginPath(ctx);

CGContextAddArc(ctx,160,400,10,0,2*M_PI,1);

CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);

CGContextFillPath(ctx);

UIImage *redBall = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

redBallView = [[UIImageView alloc] initWithImage:redBall];

redBallView.center = CGPointMake(160,300);

self.view = redBallView;

[self.view addSubview:redBallView];

//[self.view setNeedsDisplay];

[redBallView release];

pos = CGPointMake(14.0,7.0);

[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

}


About the iCodeBlog forum

Currently Online:

4 Guests

Maximum Online: 25

Forums:

Groups: 2

Forums: 6

Topics: 319

Posts: 759

Members:

There are 676 members

There are 1 guests


Brandon has made 87 posts

Top Posters:

bobcubsfan - 54

crazyiez - 30

VertigoSol - 26

AdeC - 17

Uhu - 17

Administrator: Brandon | Moderators: VertigoSol


© - Version 3.1.4 (Build 357)