1

I'm looking to create a sizeable subview that's draggable like this:

If there is an IBAction that takes you to the next View (SecondViewController) and when it does, there's another IBAction there and when you click on that one, it creates a SubView that's about half of the size of the current screen you're in (SecondViewController) that shows the third view controller that would be created? Also, how would you make that subView draggable? Thank you for helping.

1 Answers1

1

Sorry, just to be clear, you want your second view controller to have a button that when tapped, adds your third view controller taking up half the screen at the bottom?

If this is the case then you can do this with the new view controller containers in iOS5.

Ok, so you have three view controllers. For the sake of this lets say your class are called FirstViewController, SecondViewController and ThirdViewController.

I assume from what you say that you already have you instance of FirstViewController with a button, that moves you on to an instance of SecondViewController, and that the issue is then getting SecondViewController to add an instance of ThirdViewController to the bottom half of the screen when a button is pressed.

The .m file for SecondViewController will need to do something like this:

#import "ThirdViewController.h"

@interface SecondViewController ()

@property (retain) ThirdViewController *thirdViewConroller;

- (void)buttonTap;

@end

@implementation SecondViewController

@synthesize thirdViewConroller = _thirdViewConroller;

- (void)dealloc {
    self.thirdViewConroller = nil;
    [super dealloc];
}

- (void)loadView {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.titleLabel.text = @"Show third controller";
    [button addTarget:self action:@selector(buttonTap) forControlEvents:UIControlEventTouchUpInside];
    button.frame = // Some CGRect of where you want the button to be
    [self.view addSubview:button];
}

- (void)buttonTap {
    // When the button is tapped, create an instance of your ThirdViewController and add it
    self.thirdViewConroller = [[ThirdViewController alloc] initWithFrame:/* Some CGRect where you want the controller to be */ ];
    [self.thirdViewConroller willMoveToParentViewController:self];
    [self addChildViewController:self.thirdViewConroller];
    [self.thirdViewConroller didMoveToParentViewController:self];
}

@end

This should give you a button on your second controller, that will create and add you third controller. Make sure yo us till have all the standard methods that you had before, this should be in addition to what you have.

In your interface for ThirdViewController:

@interface ThirdViewController : UIViewController <NSObject>
    - (id)initWithFrame:(CGRect)frame;
@end

Then in the implementation of your ThirdViewController:

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        self.view.frame = frame;
        // Do your init stuff here
    }
    return self;
}

It should then handle adding the views and such forth.

Make sure your thirdViewController class has a valid initWithFrame: initialiser method.

This should do the trick, if you need any further help let me know :)

George Green
  • 4,807
  • 5
  • 31
  • 45
  • Forgot to mention, if you are not using ARC then you will also need to edit this to do proper memory management! – George Green Jan 29 '12 at 00:44
  • I'm not quite sure where to implement this. – user1175876 Jan 29 '12 at 01:48
  • Currently do you have 3 different view controller subclasses? This is the idea that I got form your original post? – George Green Jan 29 '12 at 02:31
  • Will update the answer, hopefully will be a bit more helpful. – George Green Jan 29 '12 at 09:03
  • SecondViewController: Warn: Instance method '-addSubView:' not found (return type defaults to 'id') Warn: 'ThirdViewController' may not respond to 'initWithFrame' ThirdViewConrtoller: Error:Receiver type 'UIViewController' for instance does not declare a method with selector 'initWithFrame:' Error & Warn: Implicit coversion of an Objective-C point to 'void' is disallowed with ARC and the warn says Void method 'initWithFrame:' should not return a value – user1175876 Jan 29 '12 at 22:16
  • Sorry that was my fault, you need to use secondViewController.view to access its's view and do addSubview on that. Also I have updated the answer so that it no longer has a void return type. – George Green Jan 29 '12 at 23:58
  • The changes that i've just made should fix both of those. It no longer tries to call initWithFrame on UIViewController and also make sure you use [self.view addSubView:button]; to add the subview. :) – George Green Jan 30 '12 at 00:04
  • What do you mean by "//Do your init stuff here"? Sorry, I'm a bit new to Objective-c and X-Code. I'm coming from Web-Design (HTML/PHP) :D. – user1175876 Jan 30 '12 at 02:07
  • Actually, what should I put for what it says " /* Some CGRect where you want the controller to be */" – user1175876 Jan 30 '12 at 03:39
  • Ok so for the first one, you need to do any other stuff that you need to do in your controller when you create it. So if there are an variables that you need to initialise etc. And for the second on you will want to do something like CGRectMake(0.0f,240.0f,320.0f,240.0f), this just defines the area of the screen that you want to put it in. So this example will make it fill the bottom half of the screen. CGRectMake(X, Y, WIDTH, HEIGHT); – George Green Jan 30 '12 at 12:45
  • Ok, that's working now I only have two warnings: Warning 1:Instance method '-addSubView:' not found (return type defaults to 'id') Warning 2: 'ThirdViewController' may not respond to 'initWithFrame:' – user1175876 Jan 31 '12 at 00:44
  • Ok so may have been a type there, try [self.view addSubview:]; with a lowercase 'v'. Also you have to make sure that you declare the method initWithFrame: in your interface section for ThirdViewController.h. – George Green Jan 31 '12 at 10:17
  • 1 Warning: Class Method '+initWithFrame:' not found (return type defaults to 'id') – user1175876 Feb 01 '12 at 04:53
  • you should be calling [[ThirdViewController alloc] initWithFrame:]; So it shouldn't be a class methods that it's trying to call. It should be an instance method. Can you post the code that you are using at present. – George Green Feb 01 '12 at 14:39