views:

140

answers:

3

Hi,

I have this app I'm working on, which on a second view asks (textfield) the name for a button to be created on first view. After specifying the name and pressing OK button, the first view pops up (as demanded) but there's no new button, although created indeed. Can I use the following code in a second view method, to "refresh" the first view before presenting itself. What's wrong with this code? Any other approach? Thank you.

-(void)initWithView:(View1Controller *)aSuperview
{
    theSuperview = aSuperview;
}

- (IBAction)itemNameButton
{
    ...
    CGRect rectang;
    rectang = CGRectMake(0, 0, 320, 460);// just in case
    [theSuperview.view setNeedsDisplayInRect:rectang];
    ...
}
A: 

You should adhere to the Model-View-Controller paradigm. Views creating buttons in other views is a bad thing in general. Instead, there should be a controller (probably a UIViewController subclass) that handles receiving the input from view 2 when the user clicks OK (via an action and a outlet to the textfield) and then tells view 1 (a custom view subclass) what to do using a defined set of methods (something like -addButtonWithTitle:(NSString *)buttonTitle). The process of adding the button itself should be fairly straight forward, something like:

- (void)addButtonWithTitle:(NSString *)buttonTitle {
    UIButton *newButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // whatever type you want

    newButton.titleLabel.text = buttonTitle;

    [self addSubview:newButton];
    newButton.center = self.center; // set your position here
}
Ben Lachman
Ben, please checkout my post.
BigJoke
A: 

Hi again, Ben, tell me please, if what I'm already doing is not what you're telling me in your post. Here's the code.

View2.h
@interface ButtonConfigViewController : UIViewController {

View1Controller *theSuperview;
UITextField* buttonNameText;//textfield receiving button's name from user

}

@property (nonatomic, retain) View1Controller *theSuperview;
@property (nonatomic, retain) IBOutlet UITextField *buttonNameText;

-(void) initWithView:(View1Controller *)aSuperview;
-(IBAction) nameButton;

@end

View2.m

-(void)initWithView:(View1Controller *)aSuperview{
theSuperview = aSuperview;
}

- (void)viewDidLoad {
    inttheSuperview=[[View1Controller alloc] init];
}

//action triggered after OK button pressed
- (IBAction)nameButton{
NSString* buttonTitle=buttonNameText.text;

[self.view removeFromSuperview]; 
[theSuperview createButton:buttonTitle];// calling (on view1) create button action similiar to that you posted
}

...and because this is not working I thougth that some kind of view1 graphics refreshing could be the solution. Once more appreciated.

BigJoke
Allocating and initialising a new view controller is very, very different to having a pointer to a view controller already in memory. Even if this wasn't the case, you really don't want to be sending messages between views in this way. You want delegation, and I've given a brief description of how this might work in my answer on this page.
David Foster
A: 

A view controller's responsibility is to control a given view. Its responsibility is not to communicate with other controllers in order to ask them to change their views, so doing this sort of thing is usually an indication of bad design.

You should have a method in your superview's controller which adds the button to its view, and then use delegation in order to be notified by your subview when it's necessary to add the button.

For a nice and simple introduction to delegates and protocols, I found this blog post to be one of the best out there.

David Foster