i have one scrollview and 4 UIviewcontrollers with xib files now i want to add 4 viewcontrollers to scrollview
and also the scroll is enabled for four viewcontrollers
any one know this plz answer this problem
Just add them. What's the problem?
// this loads a view controller from a nib
controller = [[UIViewController alloc] initWithNibName:#"YourNibsName" bundle:nil];
// this adds the viewcontroller's view to the scrollview
[scrollView addSubview:controller.view];
// place the subview somewhere in the scrollview
CGRect frame = controller.view.frame;
frame.origin.x = 600;
controller.view.frame = frame;
// don't forget to release the viewcontroller somewhere in your dealloc
[controller release];
Do this for all your four controllers.
yes, you can just add the views of the viewcontrollers to your scrolling view, but remember that you are rolling your own equivalent to a UITabBarController or UINavigationController and so you have some responsibilities:
When you alloc init your vc from a nib the vc will get its viewDidLoad method called.
But when you put the vc.view into your scrollview YOU need to call [vc viewDidAppear:YES] (and also call viewWillAppear just before if your vc uses it).
Be careful with things like presenting modal view controllers from your vc as this may not work as you expect.
Peter
I realize that it's quite an old question. But now you can use UIPageViewController.
Related
I implemented a custom tab bar controller as a set of buttons each one related to it's own View Controller. I guided on this link http://idevrecipes.com/2011/01/04/how-does-the-twitter-iphone-app-implement-a-custom-tab-bar/ to achieve the behavior. So the relevant part of code is as follows:
- (void) selectedItemAtIndex:(NSUInteger)itemIndex
{
// Get the right view controller
NSDictionary* data = [self.tabBarItems objectAtIndex:itemIndex];
UIViewController* viewController = [data objectForKey:#"viewController"];
// Remove the current view controller's view
UIView* currentView = [self.view viewWithTag:SELECTED_VIEW_CONTROLLER_TAG];
[currentView removeFromSuperview];
// Set the view controller's frame to account for the tab bar (+ 48)
viewController.view.frame = CGRectMake(0,48,self.view.bounds.size.width, self.view.bounds.size.height - 48);
// Se the tag so we can find it later
viewController.view.tag = SELECTED_VIEW_CONTROLLER_TAG;
// Add the new view controller's view
[self.view insertSubview:viewController.view belowSubview:self.tabBar];
//Keep track of current view controller
self.currentController = viewController;
}
So far is working, I can see each view controller in a similar maner to the default TabBarViewController. But then there's a requirement where I need to push a new navigation controller modally (it should take all the application frame) from inside one of the tabBar controllers.
At first glance I tried the following code from within one of the tab controllers:
DetailViewController *detailViewController = [[DetailViewController alloc]init];
UINavigationController *navigationController = [[UINavigationController alloc]detailViewController];
[self presentModalViewController:navigationController animated:YES];
However is not working as expected, first the view is shown below the TabBar and second the new view is not taking in consideration the parent view frame which should be the screen bounds less the tabbar. (0, 48, 360, 412). My detail view controller it's loading content from a nib file.
Well, this is quite obvious since the TabBar Controller is inserting each view below my custom TabBar.
[self presentModalViewController:navigationController animated:YES];
So I tried inserting it directly as a window subview:
[[UIApplication sharedApplication].keyWindow addSubview:navigationController.view];
But, I think this is not okay... there should be a better approach that I can't figure out. So if anybody could give me suggestions on how to correct or improve this navigation system it would be great.
Thanks a lot.
If you are building you app for iOS 5.0 and up you can make use of childViewController. In your custom Tab Bar you can have a containerView and a tabView.
The view of viewController is added to containerView. All the necessary events are generated to the subsequently added viewController if the following methods are implemented correctly
- (void)addChildViewController:(UIViewController *)childController;
- (void)removeFromParentViewController;
More about viewController containment can be found here.
With two ViewControllers, MyView 1 and MyView 2, is there possible to add a subview to MyView2 from MyView1.m?
I have tried:
MyView2 * screen = [[MyView2 alloc]initWithNibName:nil bundle:nil];
[screen.view addSubView:mySubView];
But my new instance of MyView 2 has no connection to the 'visible' ViewController on MyView2, right?
To clarify, the ViewController that is showing, is MyView1. I want MyView1 to be able to add a subview to the MyView2 view.
Thanks
I think you're confusing viewControllers with views, or at least your question is. Maybe it's something like this you're looking for -
MyViewController2 *myViewController2 = [[[MyViewController2 alloc] initWithNibName:nil bundle:nil] autorelease];
[myViewController2.view addSubView:mySubView];
// add any other views to myViewController2's view
[self.view addSubView:myViewController2.view]; // adding the view to VC1's view
If you want to be able to continue adding stuff throughout MyViewController1, you should declare either myViewController2 or its view as a retained property.
You could have MyView1 controller save some information in a common object in your app so that when MyView2 reappears it can add the subview to its view if needed.
Assuming MyView2 is a subclass of UIViewController, there is no addSubView: method on the viewcontroller itself. Instead, you want to add the subView to your view controller's view, like this:
[screen.view addSubview:mySubView];
try this,
[MyView1.view addSubView:MyView2];
[self.view addSubview:MyView1];
I have a viewController and I am trying to add a subview to it such that it will cover the whole screen, however this has a navigationController in it so that adding a subView always adds it below the navigation bar, is there a way to simulate a presentModalViewController in cases like this?
You can add the subview to the view controller and then hide the navigation controller from the top or you could still push it to the navigation controller and then just remove the navigation controller from the top again and then you could use pop to go back and forth.
the code to push a view controller is
if(!self.YOURVIEWCONTROLLER){
self.YOURVIEWCONTROLLER = [[YOURVIEWCONTROLLER alloc] initWithNibName:#"YOURVIEWCONTROLLER" bundle:nil] autorelease];
}
[self.navigationController pushViewController:self.YOURVIEWCONTROLLER animatedLYES];
and on the next NEXTView.m add
[self.navigationController setNavigationBarHidden:YES];
remember to create an instance of YOURVIEWCONTROLLER in the .h file. Or you could do a simple
[self.view addSubview:NEWVIEW];
[self.navigationController setNavigationBarHidden:YES];
at least at bear minimum the line for making the navigationController hide is there.
I seem to recall once having a similar problem, and I seem to recall the solution was to add the subview to the navigation controller (as the view controller is already a sub view of the navigation controller) rather than adding it to the view controller.
I recently have similar problems and after spending 5 to 10 min I get the exact solution...
According to my solution I simply add my custom UIView to subview of navigationController.view
Like This :-
[self.navigationController.view addSubview:popOver];
popOver - Your custom UIView
Happy Codding :)
Add the view to the superview of the navigationController's view.
[navigationController.view.superview addSubview:viewController.view];
Perhaps you could hide the navigation bar when you add the subview. I have a method on my ViewController that looks like this:
self.navigationController.navigationBarHidden = YES;
UIView *v = [[UIView alloc] initWithFrame:self.view.frame];
v.backgroundColor = [UIColor redColor];
[self.view addSubview:v];
When that code executes, my navigation bar disappears and a full screen red view replaces it.
I have a view that shows a map. I have a custom subclass of UIViewController (DetailViewController) that gets shown when the detailDisclosureButton of the callout above the pin is pressed. While in my map class, I create my detailview and add it to the subview like this:
DetailViewController *detailView = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
detailView.locationPoint = locationPoint;
detailView.locationCoordinate = locationCoordinate;
[self.view addSubview:detailView.view];
[detailView release];
My DetailViewController has a TableView and parses the data in DetailViewController. However I get an error of sending the numberOfSectionsInTable message to a dealloc'd instance. I'm assuming it is this since I originally had this as a property and it worked fine with (nonatomic, retain). I'm assuming that I'm releasing it before the next view is done with it. If that is the case, when would I clean up the memory??? It seems like this would be the place to do it. Thanks.
I am not sure what makes you adding the view of DetailViewController into this mapviewcontroller's view. Don't you think right approach would be to either presentModalViewController or pushNavigationController?
DetailViewController *detailView = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
detailView.locationPoint = locationPoint;
detailView.locationCoordinate = locationCoordinate;
//[self.view addSubview:detailView.view];
[self.navigationController pushViewController:detailView animated:YES];
//OR
[self presentModalViewController:detailView animated:YES];
[detailView release];
You are getting the error because you are only using the view and deallocating the view controller immediately and hence tableview datasource and delegates are hitting a nil object.
Views do not retain their view controllers. Someone needs to retain the VC or else it will get released, and then the app will crash when the view makes a call into its delegate. When you use a navigation controller, the navcon has a stack of view controllers that it retains. Likewise with presentModalViewController, the OS takes care of retaining the detail VC.
Adding a detail view as a subview is not the normal way to navigate to a new view. Instead, one either uses a navigation controller and [navcon pushViewController::], or a modal subview and [self presentModalViewController::]. If the detail view occupies only a portion of the parent view, then it is normal to retain the view controller for the subview within the parent controller. That is, within the parent VC (your map class) add a property for the detail VC. Actually, it's more common to not even use a VC for a subview, but rather for screen-filling detail views.
I have a question about UIViewController's subview, I created a UIView subclass MainView, which has the exact size of the screen, I wonder which is a better way of adding MainView, consider the following factors:
1 As MainView has same size as the whole screen, the MainView itself may have subviews, but there is no views at the save level as MainView(ie I don't need to add other subviews to self.view).
2 If I use self.view = mainView, do I put the code in loadView(as the viewDidLoad method means the view(self.view) is already loaded)? I see the loadView method is commented out by default, if I add the code to this method, what other code do I need to put together(e.g. initialize other aspects of the application)?
3 If I add mainView via [self addSubview:mainView], are there actually two off screen buffer? One for self.view, one for mainView, both has same size as the screen and one is layered on top of the other(so it wastes memory)?
Thanks a lot!
I'm not sure I completely understand what you're asking, but I'll try to answer a few of the questions you have.
First of all, if you have multiple UIViews on the screen they are all loaded into memory. You have to do -removeFromSuperview and release them to get the memory back.
You can assign your UIView as the UIViewController's view. For example:
MainView *mainView = [[MainView alloc] initWithFrame:CGRectMake(320.0, 480.0)];
self.view = mainView;
[mainView release]; //since the .view property is a retained property
in that case, you have have the view's initialization code in the -init method. Just redefine it like:
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
//initializations
}
return self;
}
You must implement loadView if you did initialize your view controller with a NIB.
UIViewController takes care of sizing its "main" view appropriately. This is all you need to do:
- (void)loadView
{
UIView* mainView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
self.view = mainView;
}
I'd solve all of this by doing it in a xib! If you create a UIView in your xib, you can then change it's class (when you select the UIView there should be a text field in the Class Identity section of the Identity inspector* - type 'MainView' here!)
Then, create your view controller by calling
myViewController = [[MainViewController alloc] initWithNibName:#"MyNibName" bundle:nil];
That should solve your problems; it's the main subview of your view controller (directly accessable from self.view) and you don't need to worry about memory usage, there's only one view :)
Sam
NB * Click tools -> Identity Inspector. I didn't know it was called this until I had to write this answer!
Yes, the first code-snippet shown above is the "standard" approach, AFAIK, when not using (evil!) NIB files -- i.e. when alloc'ing your view in-code, via loadView.
Note it seems one can also get away with the following, instead of hard-coding the screen-rect size:
UIView *myView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
self.view = myView;
[myView release];
Note you definitely want to do the [myView release] call since, indeed, as pointed out above, self.view (for UIView) is a retained property.
Cheers, -dk
Perhaps the most important thing to do is make sure you have the following:
self.view.userInteractionEnabled = YES;
While it might not be required all of the time, it fixes the issue where self.view is unresponsive. This issue pops up occasionally.