How to initialize view without known frame? - iphone

I'm building view without .xib file, just using loadView method. But in the point, when loadView is called, the frame of view is yet unknown. So, I use it just to build view hierarchy without concrete frames. (Than I update view's layout when it's frame is known.)
The question is: should I use [[UIView alloc] init] or [[UIView alloc] initWithFrame:CGRectZero] or may be something else, to initialize view without known frame?
Here is the code:
- (void)loadView
{
UIView *containerView = [[UIView alloc] init];
// or
// UIView *containerView = [[UIView alloc] initWithFrame:CGRectZero];
// or something else?
// ...
self.view = containerView;
[containerView release];
}

- (id)initWithFrame: is the designated initializer for UIView, so you should use that, with a zero-sized rect.

Related

UIScrollView covers UIButton

I have created a UIScrollView and am now trying to place a UIButton over the scroll view. However when I build and run the application the scroll view still works fine but I cannot see the UIButton.
I link the UIButton IBOutlet inside the interface builder.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scrollView.backgroundColor = [UIColor blackColor];
scrollView.delegate = self;
scrollView.bounces = NO;
backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"auckland-300.jpg"]];
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"249a-134206f1d00-1342071f5d9.ImgPlayerAUCKLAND.png"]];
// Note here you should size the container view appropriately and layout backgroundImage and image accordingly.
containerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,601,601)];
playButton = [[UIButton alloc] init]; //test button cannot see it.
[containerView addSubview:backgroundImage];
[containerView addSubview:image];
scrollView.contentSize = containerView.frame.size;
[scrollView addSubview:containerView];
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 31.0;
[scrollView setZoomScale:scrollView.minimumZoomScale];
self.view = scrollView;
}
Any help would be appreciated
Everything seems ok. But I think the reason you are not able to see the button playButton is because you are not adding it to the view itself.
Dont you need to do this ? [containerView addSubview:playButton];
To help you out, here's what I do for debugging -
UIView implements a useful description method. In addition, it
implements a recursiveDescription method that you can call to get a
summary of an entire view hierarchy.
NSLog(#"%#", [controller.view recursiveDescription]);
The button is probably in the nib that you linked the controllers view to, right? By assigning the scrollview to the view, you remove the view from
The nib from the controller, that's why you can't see te button or press it.
You can either place the button in the scrollview or you add both scrollView and thenthe playButton as subviews of self.view.
Maybe you want to rethink your design tho.
Placing a button over a scrollview doesn't really seem like good practice to me.

App freeze when I remove [super loadView]

When I remove the [super loadView]; the view wont display. The superclass is UIViewController.
- (void)loadView
{
[super loadView];
UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 367.0f) style:UITableViewStylePlain];
tableview.dataSource = self;
tableview.delegate = self;
self.tableView = tableview;
[tableview release];
[self.view addSubview:self.tableView];
}
Any idea why? Thanks in advance!
1) UIViewController Class Reference, loadView section
Your custom implementation of this method should not call super.
2) You have to set view property to something. After all this method is called loadView :). Instead of [self.view addSubview:self.tableView]; try
self.view = tableView;
If you look at the view programming guide, it mentions that if you override [loadView], you should construct your own view.
default loadView will look at bunch of stuff, like load from nib first, then construct normal view.
So, just construct a view, and assign it to self.view -
UIView *view = [[UIView alloc] initWithFrame ...];
self.view = view;
[view release];
then it should be fine.
edit: example with your code:
- (void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 367.0f)];
self.view = view;
[view release];
UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 367.0f) style:UITableViewStylePlain];
tableview.dataSource = self;
tableview.delegate = self;
self.tableView = tableview;
[tableview release];
[self.view addSubview:self.tableView];
}
edit2: link to viewcontroller programming guide:
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/BasicViewControllers/BasicViewControllers.html#//apple_ref/doc/uid/TP40007457-CH101-SW1
Look at custom view controller section, Creating the View Programmatically, and few other places in that doc.
I think you wanna move that [ tableview release ] after the addSubview.
This is because you never assign the view property in the code. When in the last line you access the view property, it causes -loadView to be called again, which results in a endless loop.

Where can I specify which UIView is used for the view property of a UIViewController?

I want to set the view property of a UIViewController at runtime. I have an .xib file with two views, and I want my UIViewController subclass that owns the .xib file to decide which UIView to use at runtime. I thought I could do this in loadView by just saying
if(some condition)
self.view = thisView;
else
self.view = thatView;
but that didn't work. How can I do this?
If you want to choose your view dynamically, set it inside -[UIViewController loadView]. A word of caution though: calling -[UIViewController view] will call -[UIViewController loadView] if the view hasn't been loaded yet, so if you do this:
-(void)loadView
{
self.view = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
self.view.backgroundColor = [UIColor redColor];
}
The second line of that method will call -loadView, and you'll get infinite recursion (which will lead to a stack overflow, and a crash). You need to setup your view, then set the .view property when you've set it up, like this:
-(void)loadView
{
UIView *newView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
newView.backgroundColor = [UIColor redColor];
self.view = newView;
}
So you'll probably want to do something like this:
-(void)loadView
{
UIView *newView = nil;
if (self.theSkyIsBlue) {
newView = [[[BlueSkyView alloc] initWithFrame:CGRectZero] autorelease];
newView.backgroundColor = [UIColor blueColor];
}
else {
newView = [[[GraySkyView alloc] initWithFrame:CGRectZero] autorelease];
newView.backgroundColor = [UIColor grayColor];
}
self.view = newView;
}
Addendum 1 - update to show how to use a container view for different views defined in a XIB
If you want to reference other stuff in your XIB, a better approach is to use your .view as a "container view" for your other views. Set it up in -viewDidLoad, like this:
- (void)viewDidLoad
{
UIView *childView = nil;
if (someCondition) {
childView = self.blueView;
}
else {
childView = self.grayView;
}
[self.view addSubview:childView];
childView.frame = self.view.bounds;
}
Note that if you want to swap your views later on, you should make childView a property, instead of a local variable, so you can remove the old childView when inserting a new one.
Inside -(void)loadView; method is where you create your view, so there is where you want to set it conditionally ;)

Creating a UITableViewController programmatically

This is what I tried. Nothing appears on the screen and none of the UITableView methods that you are supposed to implement are getting called.
-(void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = view;
[view release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UITableViewController *TVC = [[[UITableViewController alloc]
initWithStyle:UITableViewStylePlain] autorelease];
CGRect cgRct = CGRectMake(0, 10, 320, 100);
UIView *newView = [[UIView alloc] initWithFrame:cgRct];
TVC.view = newView;
[newView release];
[self.view addSubview:TVC.view];
}
I've looked for good examples and tutorials on doing this programmatically but there are none.
What I am trying to achieve is a Table that doenst take up my who screen. Maybe 3/4 of my screen would be good.
Many Thanks
Code
The problem is that you're creating a UITableViewController, which is a UIViewController, and will expect to be on the nav stack. What you want to do is create a UITableView, which is a UIView. You are also not setting the table's delegate and data source, which you will need to do to get calbacks.
Your viewDidLoad should look something like this
- (void)viewDidLoad
{
[super viewDidLoad];
UITableView *table = [[[UITableView alloc] initWithStyle:UITableViewStylePlain] autorelease];
table.dataSource = self;
table.delegate = self;
table.frame = CGRectMake(0, 10, 320, 100);
[self.view addSubview:table];
}
(Note that if you're going to need access to the table outside of the callbacks, you should save it in an ivar rather than declaring it locally, and should retain it. Let me know if you need a few more lines of code to show you what I mean)
Make sure you set the delegate of TVC, with
TVC.delegate = self;
That's the reason why none of those methods are getting called. Also, make sure your class implements the UITableViewDelegate protocol by changing your interface declaration to
#interface YourViewController : UIViewController <UITableViewDelegate> {
//declare variables here
}
Also, equally important, don't set TVC.view, as this already happens when you initialize the view controller. You're just setting it to a blank view, which is why you're not seeing anything.
iOS7 seems to like this way of init'ing the tableview:
//make tableview
UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 81, 200, 200) style:UITableViewStylePlain];
table.dataSource = self;
table.delegate = self;
[self.dataView addSubview:table];
try that out. Hope it helps someone.
UIView *newView = [[UIView alloc] initWithFrame:cgRct];
TVC.view = newView;
I'll give you a hint. Here you are setting the view of the UITableViewController to an EMPTY VIEW...

adding uiview as a subview to the main view

i want to add an UIView for small size as a subview to existing view.
i have a UIViewController pushed with a nib name which is the main view, i am trying to create an object of UIView and alloc it with dimension with the following code in the viewcontroller's viewDidLoad methoad
UIView *view = [[UIView alloc] init];
but the idea is not suggesting the alloc on UIView and throwing an error if i forcefully type and run
please help me out with this
To add something (rather important) to the answers above;
CGRect frame = CGRectMake(x, y, width, height); // Replacing with your dimensions
UIView *view = [[UIView alloc] initWithFrame:frame];
Then, you want to actually add it to the superview (assuming the view is self.view)
[self.view addSubview:view];
UIView needs to be alloc'ed and init'ed with a frame:
CGRect frame = CGRectMake(x, y, width, height); // Replacing with your dimensions
UIView *view = [[UIView alloc] initWithFrame:frame];
UIView *view = [[UIView alloc] init];