views:

226

answers:

1

Hi all,

I haven't been programming on the iPhone for very long, but I'm picking it up slowly by googling problems I get. Unfortunately I haven't been able to find an answer for these.

I have started a new View-based application in Xcode 3.2.2 and immediately added the following files: myUIView.m and myUIView.h, which are subclasses of UIView. In Interface Builder, I set the subclass of the default UIView to be myUIView.

I made a button in the drawRect method.

Problem one: The title of the button only appears AFTER I click the screen, why?

Problem two: I want the button to produce the modalview - is this possible?

The code is as follow:

#import "myUIView.h"
@implementation myUIView
- (void)drawRect:(CGRect)rect {
// Drawing code
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(0,0,100,100);
    [button setTitle:@"butty" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
}

-(void)buttonPressed:(id)sender{
    NSLog(@"Button pressed");
    //present modal view somehow..?
}

I can't see how to post attachments, but if anyone thinks it will help I can upload the source.

Many thanks, Andy

+1  A: 

drawRect: is for custom drawing. You won't often need to do that. Your button won't be drawn until it gets sent drawRect:, probably in the next iteration of the run loop (ie, when you click on the view).

If you want a button on your view, either drag it there using IB, or move the code from drawRect: to viewDidLoad.

You present a modal view from a view controller by:

[self presentModalViewController:viewController animated:YES];

Do this in the action method your button sends to your view controller. This should indicate to you that you need to take a look at your application design.

Paul Lynch
Ok that's a good method. It does seem to draw the button and background colour, just not the title or background image...Any ideas for my second problem?Thanks, A
Andy
Answer edited...
Paul Lynch
It seems that viewDidLoad isn't a method a subclass of UIView responds to.I put the button in awakeFromNib instead. My code now looks like this:-(void)buttonPressed:(id)sender{ newNib *myModalVC = [[newNib alloc] init]; [self presentModalViewController:myModalVC animated:YES];}However, this doesn't work as "'myUIView' may not respond to '-presentModalViewController:animated:'"...The project requires several buttons being created dynamically, so I suppose they have to be made in the UIView subclass, but I cannot call the modal view as that requires a UIViewController..*stuck :-?*
Andy
Note the last comment in my answer. You really should have a view controller. The role of a view is to draw itself; the view controller is responsible for transitions and coordinating data. It is likely that the code you have really belongs in a view controller.
Paul Lynch
Thanks for that Paul; I did try putting it all in the ViewController, and making the button in the viewDidLoad method, however I run in to further problems because the UIViewController doesn't like "[self addSubview:button];" --- presumably there is an alternate method of creating the button I have yet to find..?Thanks for helping me with this. A
Andy
A viewcontroller has a view property, therefore:[self.view addSubview:button];will work.
Paul Lynch
Brilliant, Paul --- Thanks!
Andy