Subscribe ( )

iPhone Programming Tutorials


 

Forum

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];

}

Search 

About the iCodeBlog forum

Most Users Ever Online:

44


Currently Online:

2 Guests

Forum Stats:

Groups: 2

Forums: 6

Topics: 419

Posts: 893

Membership:

There are 934 Members

There has been 1 Guest

There are 2 Admins

There is 1 Moderator

Top Posters:

bobcubsfan – 54

crazyiez – 30

Uhu – 17

AdeC – 17

Nick – 15

jitesh61 – 12

Administrators: Brandon (88 Posts), Collin (0 Posts)

Moderators: VertigoSol (26 Posts)



©   

  • Posted by on 6 Aug 2008 in Uncategorized
  •   |  
  •   |  
  •   |  
  • Comments Off

Comments are closed.