I have one screen where I want to show master-detail interface. But I need master to be hideable with animation. As far as I know UISplitViewController doesn't support that.
So, instead I made one UIViewController and added two child controllers to him.
In this container view controller I do:
- (id)init
{
self = [super init];
if (self) {
self.masterViewController = [[MasterViewController alloc] init];
[self addChildViewController:self.masterViewController];
self.detailViewController = [[DetailViewController alloc] init];
[self addChildViewController:self.detailViewController];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.masterViewController.view.frame = CGRectMake(0, 0, 256, 748);
[self.view addSubview:self.masterViewController.view];
self.detailViewController.view.frame = CGRectMake(256, 0, 768, 748);
[self.view addSubview:self.detailViewController.view];
}
But the result is a complete mess. It doesnt change frame properly.
What is the correct way to mimic master-detail properly when I have two view controllers (each one has his own superclass)?
Related
I want to add a view that will persist through out the application?
How can i achieve this?
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 430, 320, 50)];
myView.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.5];
[self.window addSubview:myView];
This is not working.
Thanks in advance...
There are several ways in achieving that. It's not clear if you want a view like that be in each view controller, or you want the same instance in each view controller.
Since I don't see any reason to have one shared instance (basing on your description), my approach would be to subclass UIViewController, let's call it SOMyViewController, and then inherit all view controllers in your app from SOMyViewController.
Then, I'd override the 'viewDidLoad' method of SOMyViewController as follows:
- (void) viewDidLoad {
[super viewDidLoad];
if ([self addMyCustomView]) {
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 430, 320, 50)];
myView.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.5];
[self.window addSubview:myView];
}
}
/**
Override this in all your subclasses to decide whether to display the custom view or not
*/
- (BOOL) addMyCustomView {
return YES;
}
If instead you want the same instance shared among view controllers, I would change the above code as follows:
static UIView *mySharedView;
+ (void) initialize {
mySharedView = [[UIView alloc]initWithFrame:CGRectMake(0, 430, 320, 50)];
mySharedView.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.5];
}
- (void) viewDidLoad {
[super viewDidLoad];
if ([self addMyCustomView]) {
[self.window addSubview:mySharedView];
}
}
/**
Override this in all your subclasses to decide whether to display the custom view or not
*/
- (BOOL) addMyCustomView {
return YES;
}
I'm trying to use a scroll view to have pagination with pages of subviews that are images that can be pinched zoomed on iOS. The pagination works, but as soon as an image is pinch-zoomed, the app crashes with EXEC_BAD_ACCESS(code=1,address=...)
I'm aware that it's a bit odd to swipe a zoomed image to pan the image and also swipe to paginate, but in the real app, the pagination will be done with a page control. Also I think it could work like the preview app. If an image is zoomed, panning will go down to the bottom of the image and then after that is reached, it goes to the next image.
Is this possible?
Here's an example:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ScrollerViewController *viewController = [[ScrollerViewController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
ScrollerViewController.m - the outer pagination view controller
- (void)viewDidLoad {
[super viewDidLoad];
// outer scroll view for paging with two pages
CGRect frame = CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height);
UIScrollView *pagingScroller = [[UIScrollView alloc] initWithFrame:frame];
pagingScroller.pagingEnabled = YES;
pagingScroller.scrollsToTop = NO;
pagingScroller.userInteractionEnabled = YES;
pagingScroller.contentSize = CGSizeMake(self.view.bounds.size.width*2,self.view.bounds.size.height);
// first page
ImageViewController *page1 = [[ImageViewController alloc] init];
page1.filename = #"cat.jpg";
page1.view.frame = CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height);
[pagingScroller addSubview:page1.view];
// second page
ImageViewController *page2 = [[ImageViewController alloc] init];
page2.filename = #"dog.jpg";
page2.view.frame = CGRectMake(self.view.bounds.size.width,0,self.view.bounds.size.width,self.view.bounds.size.height);
[pagingScroller addSubview:page2.view];
self.view = pagingScroller;
}
ImageViewController.m - the pinch-zoom image
- (void)viewDidLoad {
[super viewDidLoad];
// scroll view for pinch zooming
CGRect frame = CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height);
UIScrollView *zoomScroller = [[UIScrollView alloc] initWithFrame:frame];
zoomScroller.minimumZoomScale = 1.0;
zoomScroller.maximumZoomScale = 5.0;
zoomScroller.userInteractionEnabled = YES;
zoomScroller.delegate = self;
imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.userInteractionEnabled = YES;
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [UIImage imageNamed:filename];
[zoomScroller addSubview:imageView];
self.view = zoomScroller;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
The full project is at https://github.com/tomkincaid/ZoomScrollTest
I can test that the pinch zoom works by changing
ScrollerViewController *viewController = [[ScrollerViewController alloc] init];
to
ImageViewController *viewController = [[ImageViewController alloc] init];
viewController.filename = #"cat.jpg";
Its been quite a while that you posted your question. I bet you fixed it already yourself but I want to make sure other people can use your code.
However I downloaded your small GitHub project and found that you get the crash because you don't retain the ImageViewController's page1 and page2 in [ScrollerViewController viewDidLoad]. The views them selfs don't retain their controllers therefor the controllers get released after viewDidLoad in your case. Then when you pinch on the image scroll view it calls for its delegate but it is already deallocated.
To fix this I added two ImageViewController properties to the ScrollerViewController class and stored the controller objects there.
#interface ScrollerViewController ()
#property (strong) ImageViewController *page1;
#property (strong) ImageViewController *page2;
#end
In [ScrollerViewController viewDidLoad] I added at the end:
self.page1 = page1;
self.page2 = page2;
I hope that someone may find this information useful. Maybe you want to update your GitHub project so that it will compile and run.
I am creating an app using iOS 5, and I want to work as follows:
MainViewController w/ NavigationBar, and i set on viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
NavViewController *nav = [[NavViewController alloc] init];
[self addChildViewController:nav];
[self.view addSubview:nav.view];
}
Like a "partial view" with some nav controls (my own/custom toolbar), and at NavViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect viewRect = CGRectMake(0, 0, 320, 200);
self.view.frame = viewRect;
self.view.backgroundColor = [UIColor redColor];
}
At this point I have MainViewController with a navigation subview and still have space (in MainViewController view) that another subview can be added using the actions NavViewController.
Is there a serious flaw in this logic? or I can keep developing ?
Thnks a lot.
I have 2 view controllers, the first is a storyboard (this is root) and the second with is nibless. When I press a button in the root view controller it should call the second controller.
Here the code for my second view controller:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UILabel *sampleLabel = [[UILabel alloc] initWithFrame: CGRectMake(0,0,100,100)];
UIImageView * basketItem = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"B.jpg"]];
[self.view addSubview:sampleLabel];
[self.view addSubview:basketItem];
NSLog(#"%#",self.view.subviews);
sampleLabel.text = #"Main Menu";
}
return self;
}
self.view.sebviews query shows that 2 objects label and imageView objects exists, but in fact I see black screen only.
Here is transition method
- (void)transitionToViewController:(UIViewController *)aViewController
withOptions:(UIViewAnimationOptions)options
{
aViewController.view.frame = self.containerView.bounds;
[UIView transitionWithView:self.containerView
duration:0.65f
options:options
animations:^{
[self.viewController.view removeFromSuperview];
[self.containerView addSubview:aViewController.view];
}
completion:^(BOOL finished){
self.viewController = aViewController;
}];
}
Move the code in viewDidLoad. Here you are sure the view has been loaded into memory and hence can be further customized.
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *sampleLabel = [[UILabel alloc] initWithFrame: CGRectMake(0,100,100,100)];
UIImageView * basketItem = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"B.jpg"]];
[self.view addSubview:sampleLabel];
[self.view addSubview:basketItem];
NSLog(#"%#",self.view.subviews);
sampleLabel.text = #"Main Menu";
}
If you are not using ARC, pay attention to memory leaks.
Note
I really suggest to read Apple doc for this. You should understand how things work. Hope that helps.
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html
Edit
I don't know what the problem could be. To make it work, try to override loadView (in MenuViewController) method like the following:
- (void)loadView
{
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIView *contentView = [[UIView alloc] initWithFrame:applicationFrame];
contentView.backgroundColor = [UIColor redColor]; // red color only for debug purposes
self.view = contentView;
}
Leave the viewDidLoad method as I wrote and see what happens.
When you create the view controller use only init method.
MenuViewController *vc = [[MenuViewController alloc] init];
Your UILabel's frame has size.width=0:
CGRectMake(0,100,0,100)
and if B.jpg is not added to the project you UIImageView will also be empty.
Also, if second UIViewController doesn't have a XIB, initialize it using the - (id)init method instead of - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil.
I watched the WWDC video on UIViewController Containment and read through this blog post: http://www.cocoanetics.com/2012/04/containing-viewcontrollers/
but I can't get my initial view controller to show. Is there something I am missing? In my ContainerViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_homeViewController = [[HomeViewController alloc] init];
_detailViewController = [[DetailViewController alloc] init];
[self setSubViewControllers:#[_homeViewController, _detailViewController]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (_selectedViewController.parentViewController == self) {
// nothing to do
return;
}
// adjust the frame to fit the container view
_selectedViewController.view.frame = _containerView.bounds;
// make sure that it resizes on rotation automatically
_selectedViewController.view.autoresizingMask = _containerView.autoresizingMask;
// add as child VC
[self addChildViewController:_selectedViewController];
// add it to container view, calls willMoveToParentViewController for us
[_containerView addSubview:_selectedViewController.view];
// notify that move is done
[_selectedViewController didMoveToParentViewController:self];
}
- (void)loadView {
// set up the base view
CGRect frame = [[UIScreen mainScreen] bounds];
UIView *aView = [[UIView alloc] initWithFrame:frame];
aView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
aView.backgroundColor = [UIColor blueColor];
// set up content view
_containerView = [[UIView alloc] initWithFrame:frame];
_containerView.backgroundColor = [UIColor grayColor];
_containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[aView addSubview:_containerView];
self.view = aView;
}
- (void)setSubViewControllers:(NSArray *)subViewControllers {
_subViewControllers = [subViewControllers copy];
if (_selectedViewController) {
// remove previous VC
}
_selectedViewController = _subViewControllers[0];
}
My ContainerViewController is the initial view controller in my storyboard. I see that it shows on the simulator, but the HomeViewController (the first child view controller in my container) does not show.
When I step through the debugger, the subViewControllers property of my ContainerViewController does have the homeViewController and detailViewController in it. The viewDidLoad of HomeViewController also does get called. I just don't see anything on screen except the background color of the ContainerViewController.
Any thoughts? Thanks.
So I'm not the brightest person in the world, but the reason nothing was being shown on the screen was because the nibs were in the storyboard and I needed to do this instead:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPad" bundle:nil];
_homeViewController = [storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"];
_detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
Hopefully this helps someone who is also not familiar with Storyboards yet.
You have an NSArray, but you are trying to access it as a C array.
_subViewControllers[0]
should be:
[_subViewControllers objectAtIndex:0];
That being said, you seem to have some code that could be better in other methods. I would personally clean this up a lot and make it much simpler. I would remove loadView and _containerView, and just use self.view as one normally would. For what you are trying to do, there really doesn't even seem a need to track parent and child view controllers. Anyway, this is how I would do it:
#interface ContainerViewController ()
#property (nonatomic, retain) NSArray *subViewControllers;
#end
#implementation ObservationReportViewController {
UIViewController *_selectedViewController;
}
#synthesize subViewControllers = _subViewControllers;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
HomeViewController *homeViewController = [[HomeViewController alloc] init];
homeViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
DetailViewController *detailViewController = [[DetailViewController alloc] init];
detailViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// Retain the view controllers.
self.subViewControllers = #[homeViewController, detailViewController];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self setSelectedViewController: [_subViewControllers objectAtIndex:0]];
}
-(void)setSelectedViewController:(UIViewController *)selectedViewController {
if (_selectedViewController != selectedViewController) {
[_selectedViewController.view removeFromSuperview];
_selectedViewController = selectedViewController;
// adjust the frame to fit the container view
[self.view addSubview:_selectedViewController.view];
//_selectedViewController.view.frame = _containerView.bounds;
_selectedViewController.view.frame = self.view.bounds;
}
}
If you set the InitialViewController through the storyboard in a different storyb than the MainStoryboard, then you need to update the project settings to use that new storyboard.
Go to project settings, General and set the Main Interface setting to the new storyboard