iOS accessing view's variable from view controller - iphone

I'm developing on iOS and I'm building my views programmatically. I noticed that when I try to access variables that have to be changed in my view from the view controller they are null. I'll post both the view and its view controller:
RootViewController.h
#import <UIKit/UIKit.h>
#class RootView;
#interface RootViewController : UIViewController {
RootView *rootView;
}
#property (nonatomic,retain) RootView *rootView;
#end
RootViewController.m
#import "RootViewController.h"
#import "RootView.h"
#implementation RootViewController
#synthesize rootView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[rootView release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)loadView{
RootView *rootV = [[RootView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
rootV.rootViewController = self;
self.view = rootV;
[rootV release];
}
- (void)viewDidLoad{
NSLog(#"TEXT: %#",self.rootView.label.text);
self.rootView.label.text=#"HELLO!";
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[self setRootView:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
RootView.h
#import <UIKit/UIKit.h>
#class RootViewController;
#interface RootView : UIView {
RootViewController *rootViewController;
UILabel *label;
}
#property (nonatomic,assign) RootViewController *rootViewController;
#property (nonatomic,retain) UILabel *label;
#end
RootView.m
#import "RootView.h"
#import "RootViewController.h"
#implementation RootView
#synthesize rootViewController;
#synthesize label;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//Create the label
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100,100, 50)];
//Set the font bold
testLabel.font = [UIFont boldSystemFontOfSize:20.0];
//Set the backgroundcolor of the label to transparent
testLabel.backgroundColor = [UIColor clearColor];
//Set the text alignment of the text label to left
testLabel.textAlignment = UITextAlignmentLeft;
//Set the text color of the text label to black
testLabel.textColor = [UIColor blackColor];
testLabel.text = #"01:30";
self.label = testLabel;
[self addSubview:label];
[testLabel release];
}
return self;
}
- (void)dealloc
{
[label release];
rootViewController = nil;
[super dealloc];
}
#end
I changed the code but it seems not working.....
Ok solved I forgot this line "self.rootView = rootV;"

Your view doesn't find out what its controller is until after its -initRootView method returns, but you're trying to use the controller from within that method.
That said, it would be much better if you followed the usual Cocoa Touch pattern for a view controller creating its view. View controllers are supposed to create their views lazily, which is to say that they defer view creation and initialization until the -loadView method is called. You can override -loadView to create your view, and also override -viewDidLoad to do any setup work that needs to be done after the view is created.
Also, it's generally not advisable for a view to know about its controller. The controller should tell the view what to do, not the other way around. If you need the view to send some information to the controller, you usually provide the controller to the view as the view's delegate. But if you just need the view controller to be able to find some subview, like your label, it's probably a good idea to either provide some accessors in the container view for that (so that the view controller can just say something like self.view.label.text = #"some text";. Another options is to set the subview's tag property to some unique value and have the controller use that to find the subview.

The problem is easy to spot, but requires some work to fix.
Looking at your code, something that I immediately want to suggest is to put all your RootView initialization code the loadView method of your RootViewController. That's where it should be (see here why).
Also, if you absolutely need your RootView to have a reference back at RootViewController, you should probably do that in viewDidLoad. But I wouldn't recommend doing that.
When using the MVC pattern, it is the controller's responsibility to initialize and update views. The line self.rootViewController.rootViewLabel = testLabel; should be removed from RootView's implementation. It's not clear what your intention is there, but if you want the rootViewLabel updated, you should let the controller do that.
To sum it all up:
// RootViewController.m
- (id)initRootViewController{
self = [super init];
if(self){
// other init code here
}
return self;
}
- (void)loadView {
RootView *rootV = [[RootView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
self.view = rootV;
[rootV release];
}
- (void)viewDidLoad {
[super viewDidLoad];
// etc...
}
// etc.
Now, as for RootView, here is what it would look like:
RootView.h
#import <UIKit/UIKit.h>
#interface RootView : UIView {
UILabel *rootViewLabel;
}
// moved from RootViewController
#property (nonatomic, retain) UILabel *rootViewLabel;
#end
RootView.m
#import "RootView.h"
#implementation RootView
#synthesize rootViewLabel;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Whatever initialization code you might have
//Create the label
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100,100, 50)];
//Set the font bold
testLabel.font = [UIFont boldSystemFontOfSize:20.0];
//Set the backgroundcolor of the label to transparent
testLabel.backgroundColor = [UIColor clearColor];
//Set the text alignment of the text label to left
testLabel.textAlignment = UITextAlignmentLeft;
//Set the text color of the text label to black
testLabel.textColor = [UIColor blackColor];
testLabel.text = #"01:30";
self.rootViewLabel = testLabel;
[testLabel release];
// add rootViewLabel as a subview of your this view
[self addSubView:rootViewLabel];
}
return self;
}
- (void)dealloc
{
[rootViewLabel release];
[super dealloc];
}
#end
I hope this gives you an idea on how to structure your view initialization code...
(Disclaimer, I can't test this code now, please point out any errors! Thanks)

Not sure why you're doing it like that, but you could probably make it work if you pass the controller into the view's init:
RootView *rootV = [[RootView alloc] initRootView:self];
view's init:
- (id)initRootView:(UIViewController*)controller
{
self.rootViewController = controller;
self.rootViewController.rootViewLabel = testLabel;

Related

Add UISearchBarController to a UITableViewController Without IB

I have a UITableViewController and I want to add a UISearchBarController at the top so it searches with a different table view (not the table view of the UITableViewController).
How can I initialize this via code and no IB?
#interface mySearchController : UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate>
#property (nonatomic, retain) UISearchDisplayController *aSearchBarController;
#property (nonatomic, retain) UISearchBar *aSearchBar;
#end
- (id)init {
if ((self = [super init])) {
UISearchBar *tempSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 0)];
self.aSearchBar = tempSearchBar;
self.aSearchBar.delegate = self;
[self.aSearchBar sizeToFit];
self.tableView.tableHeaderView = self.aSearchBar;
[self.aSearchBar release];
UISearchDisplayController *tempSearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:aSearchBar contentsController:self];
self.searchDisplayController = tempSearchDisplayController;
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
}
return self;
}
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
// Custom initialization.
}
return self;
}
A cursory glance at the UISearchDisplayController Class Reference would answer your question.
"Typically you initialize a search display controller from a view
controller (usually an instance of UITableViewController) that’s
displaying a list. To perform configuration programmatically, set self for the search display controller’s view controller and search results data source and delegate."
So it should look like this:
searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
If you follow this pattern, then in the table view data source and delegate methods you can check the methods’ table view argument to determine which table view is sending the message:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableView)
{
return ...;
}
// If necessary (if self is the data source for other table views),
// check whether tableView is searchController.searchResultsTableView.
return ...;
}

Can't dismiss modal view when tap a button

In "FirstViewController" I declare a button which present the modal view "InfoViewController".
In "InfoViewController", I declare a toolbar with a "modalViewButton" UIButton which dismiss the modal view. But the "OK" UIButton doesn't work. I don't know why.
Here's FirstViewController.h
#import <UIKit/UIKit.h>
#import "InfoViewController.h"
#interface FirstViewController : UIViewController
{
InfoViewController *infoViewController;
}
#property (nonatomic, retain) InfoViewController *infoViewController;
#end
Here's FirstViewController.m
#import "FirstViewController.h"
#implementation FirstViewController
#synthesize infoViewController;
- (IBAction)modalViewAction:(id)sender
{
if (self.infoViewController == nil)
self.infoViewController = [[[InfoViewController alloc] initWithNibName:
NSStringFromClass([InfoViewController class]) bundle:nil] autorelease];
[self presentModalViewController:self.infoViewController animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[infoViewController release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* modalViewButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[modalViewButton addTarget:self
action:#selector(modalViewAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:modalViewButton];
self.navigationItem.leftBarButtonItem = modalBarButtonItem;
[modalBarButtonItem release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Here's InfoViewController.h
#import <UIKit/UIKit.h>
#interface InfoViewController : UIViewController
{
}
-(IBAction)infoDismissAction:(id)sender;
#end
Here's the InfoViewController.m
#import "InfoViewController.h"
#implementation InfoViewController
- (IBAction)infoDismissAction:(id)sender
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *infoLabel = [[UILabel alloc] init];
infoLabel.frame = CGRectMake(50, 100, 100, 40);
infoLabel.textAlignment = UITextAlignmentCenter;
infoLabel.text = #"About";
[self.view addSubview:infoLabel];
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil] autorelease];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"OK"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(infoDismissAction:)];
UIBarButtonItem* infoTitle = [[UIBarButtonItem alloc] initWithTitle:#"About"
style:UIBarButtonItemStylePlain
target:self action:nil];
NSArray *barButtons = [[NSArray alloc] initWithObjects:flexibleSpace,flexibleSpace,infoTitle,flexibleSpace,doneButton,nil];
[toolBar setItems:barButtons];
[self.view addSubview:toolBar];
[toolBar release];
[infoTitle release];
[doneButton release];
[barButtons release];
[infoLabel release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
I would solve this issue with a delegate method.
First make a protocol in your modalViewController
#protocol ModalViewDelegate <NSObject>
- (void)didDismissModalView;
#end
And set a delegate property in the same modalVC:
id<ModalViewDelegate> dismissDelegate;
Then make a buttonActionMethod that calls the delegate in the modalVC:
- (void)methodCalledByButton:(id)sender
{
// Call the delegate to dismiss the modal view
[self.dismissDelegate didDismissModalView];
}
Now your modalVC is done you have to prepare the mainVC calling the modalVC:
You have to make your MainViewController comform to the delegate:
#interface MainViewController : UIViewController <ModalViewDelegate>
At the place you alloc your ModalViewController you have to set the delegate property you made in your modalViewController:
self.myModalViewController.dismissDelegate = self;
Now the MainViewController listens to the delegate and the only thing you need to do is implement the delegateMethod.
-(void)didDismissModalView
{
[self dismissModalViewControllerAnimated:YES];
}
Now your ModalVC will dismiss on a buttonpress (at least when you call the method properly)
Hope this all makes sense.
Good luck.
You can only dismiss currently displayed modal view, so in your method infoDismissAction: you should do one of following
1) [self dismissModalViewControllerAnimated:YES];
2) Send to parent view controller message that current modal view should be dismissed and send reference to that view.
Second approach is better as it is more safe.
In your -infoDismissAction try to call [self dismissModalViewControllerAnimated:YES];
Here the best sample code for the model view for iphone and ipad also.
The popups have a number of configurable items. They can be animated to either slide or popup onto the display. Once visible, they can be dismissed either by tapping the screen or after a programmed delay. The background and text colors can also be adjusted however you like.
Download the sample code from here.
The current answers are deprecated. Here is the updated code:
[self dismissViewControllerAnimated:NO completion:nil];

Can't add UINavigationController programmatically to a TabBar Application

I have an application for which I use TabBar template. In one of viewcontrollers I want to add a uinavigationcontroller. I declare it in the .h file;
#import <UIKit/UIKit.h>
#import "AnotherViewController.h"
#interface SecondViewController : UIViewController <UINavigationControllerDelegate> {
UIButton *UIButton *gotoAnotherView;;
AnotherViewController *anotherView;
UINavigationController *navigationController;
}
#property(nonatomic,retain) UIButton *UIButton *gotoAnotherView;;
#property(nonatomic,retain) AnotherViewController *anotherView;
#property(nonatomic,retain) UINavigationController *navigationController;
-(void)buttonPressed:(id)sender;
#end
And here's my .m file
#import "SecondViewController.h"
#implementation SecondViewController
#synthesize navigationController, anotherView, gotoAnotherView;
-(void)buttonPressed:(id)sender {
anotherView = [[AnotherViewController alloc]init];
[navigationController pushViewController:anotherView animated:YES];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
navigationController = [[UINavigationController alloc ]initWithRootViewController:self];
[navigationController.navigationBar setFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:navigationController.navigationBar];
gotoAnotherView = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 40, 40)]; //kategoributonlari
UIImage *image = [UIImage imageNamed:#"1.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(110, 5, 100, 20);
[self.view addSubview:imageView];
[kategori1 setBackgroundImage:image forState:UIControlStateNormal];
[kategori1 addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:kategori1];
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
However I can see from the navigation bar that the navigationcontroller goes one level deeper(back button appears) but the main view remains the same with my gotoAnotherView button.
I think that I might not make the navigationcontroller control the whole view.
Instead of trying to do this in code, edit the XIB for your main window (with the UITabBarController). Drag out a UINavigationController from the Library onto the tab bar. This will create a new bar item for you, with a UINavigationController. Select the UIViewController nested in the new UINavigationController, and on the Identity tab set the Class to your view controller, and on the Attributes tab specify the name of the nib file to load.
You don't need to use IB. You can setup everything in code. First create your view controllers tab1ViewController, tab2ViewController, etc. then create the navigation controller with the root view controllers of tab1ViewController etc. and then add these controllers to the tab bar controller.
Here is a sample:
UINavigationController *tab1NavigationController = [[UINavigationController alloc] initWithRootViewController:tab1ViewController];
UINavigationController *tab2NavigationController = [[UINavigationController alloc] initWithRootViewController:tab2ViewController];
UITabBarController rootViewController = [[UITabBarController alloc] init];
rootViewController.viewControllers = [NSArray arrayWithObjects:tab1NavigationController, tab2NavigationController, nil];
[tab1NavigationController release];
[tab2NavigationController release];

Why Isn't My Info Button Working?

My Info button is showing up when I run my app but it doesn't do anything, no response when I click it.
In my ProfileViewController file:
- (void)viewDidLoad
{
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
infoButton.frame = CGRectMake(290.0, 10.0, 15.0, 15.0);
[infoButton addTarget:self action:#selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:infoButton];
[super viewDidLoad];
}
I also have the following two methods to load the about view (the screen that loads up when the button is clicked):
- (IBAction) toggleCreditsOpen:(id)inSender
{
UIViewController *theController = [[UIViewController alloc] initWithNibName:#"AboutViewController" bundle:nil];
[self.navigationController presentModalViewController:theController animated:TRUE];
}
- (IBAction) toggleCreditsClosed:(id)inSender
{
[self.navigationController dismissModalViewControllerAnimated:TRUE];
}
EDIT:
I am adding my full implementation file here:
#import "ProfileViewController.h"
#import "AboutViewController.h"
#implementation ProfileViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (IBAction)toggleCreditsOpen:(id)inSender
{
UIViewController *theController = [[UIViewController alloc] initWithNibName:#"AboutViewController" bundle:nil];
[self.navigationController presentModalViewController:theController animated:YES];
}
- (IBAction)toggleCreditsClosed:(id)inSender
{
[self.navigationController dismissModalViewControllerAnimated:TRUE];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
UIButton *infoButton = [[UIButton buttonWithType:UIButtonTypeInfoDark] retain];
infoButton.frame = CGRectMake(290.0, 10.0, 15.0, 15.0);
//[infoButton addTarget:self action:#selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchUpInside];
[infoButton addTarget:self action:#selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchDown];
[self.view addSubview:infoButton];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Using your provided code works for me, if self.navigationController is non-nil, and there exists an AboutViewController.xib in your main bundle. Otherwise, I don't see anything readily wrong.
If you don't have a navigation controller, the proper line in toggleCreditsOpen: would be:
[self presentModalViewController:theController animated:YES];
Edit:
If you want to dismiss the modal view later, its usually a good idea to do this with a delegate. Instead of
UIViewController *theController = [[UIViewController alloc] initWithWhatever];
you can do
AboutViewController *theController = [[AboutViewController alloc] initWithWhatever];
where AboutViewController has a delegate. Probably something like id<DismissModalProtocol> delegate;. Then, your view controller would implement this #protocol, and before it calls presentModalViewController:animated:, it'll set itself as the delegate. So very roughly, it would look something like this:
// AboutViewController.h
#protocol DismissModalProtocol;
#interface AboutViewController : UIViewController {
id<DismissModalProtocol> delegate;
}
#property id<DismissModalProtocol> delegate;
#end
#protocol DismissModalProtocol <NSObject>
- (void)dismissController:(UIViewController *)viewController;
#end
// ProfileViewController.h
#import "AboutViewController.h"
#interface ProfileViewController : UIViewController <DismissModalProtocol>
#end
// ProfileViewController.m
#implementation ProfileViewController
- (void)dismissController:(UIViewController *)viewController {
[self dismissModalViewControllerAnimated:YES];
}
- (void)toggleCreditsOpen:(id)sender {
AboutViewController *controller = [[AboutViewController alloc] init];
controller.delegate = self;
[self presentModalViewController:controller animated:YES];
}
#end
I don't know at all why this isn't working, but if I were to take a guess, I'd say it's in the [self.navigationController presentModalViewController:theController animated:TRUE];
statement. From what I know, it should be:
[self.navigationController presentModalViewController:theController animated:YES];
See if it works after you do that. I'm not sure, but that could be the problem with your code.
hi
you are took a mistake to define your button..
just assign retain property at the end of button
like
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark] retain];
and also give
[infoButton addTarget:self action:#selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchDown];

Hiding master view in split view app..?

I have created split view based ipad app, where master view is table view while Detail view display images.. I need to display the image fit to screen 100% in landscape mode.
This could be on button event or double tap event.. How should i do that.
Thanks in advance.
You can accomplish what you want by using a secondary window in your app that you display on-demand on top of your main window that contains the split view.
Create a new UIWindow & a new UIViewController. Add the UIViewController's view to your new window, set the window level to a positive value (1 or more) so that it is on top of your main window, then put the new window onscreen. If you set the window background color to [UIColor clearColor] and position your image in a view inside the new UIViewController directly on top of the image that is in the detail view then the user won't notice that anything new has happened. You can then animate the image frame up to fullscreen or do whatever you want. We sometimes use this technique to support drag & drop or our own custom modal view controllers but it'll work for your purpose too.
Here's an example:
#interface MyViewController : UIViewController #end
#interface AppDelegate : NSObject <UIApplicationDelegate> {
MyViewController *overlayController;
UIWindow *overlayWindow;
UIWindow *window; // the main window that contains your splitview
UINavigationController *navigationController; // or split view contoller, whatever, your main controller
}
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
#implementation MyViewController
- (void) loadView {
self.view = [[[UIView alloc] initWithFrame: CGRectZero] autorelease];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.view.backgroundColor = [UIColor redColor];
}
#end
#implementation AppDelegate
#synthesize window, navigationController;
- (void) click:(id) sender {
[overlayController.view removeFromSuperview];
[overlayController release];
overlayController = nil;
overlayWindow.hidden = YES;
[overlayWindow release];
overlayWindow = nil;
}
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Add the navigation controller's view to the window and display.
// standard stuff...
[self.window addSubview: navigationController.view];
[self.window makeKeyAndVisible];
// add the overlay window
// note that both the overlay window and controller are retained until we dismiss
// the window, this is important!
overlayWindow = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].applicationFrame]; // or [UIScreen mainScreen].bounds, depending on what you want
overlayController = [MyViewController new];
overlayController.view.frame = overlayWindow.bounds;
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[button addTarget: self action: #selector(click:) forControlEvents: UIControlEventTouchUpInside];
[button setTitle: #"Done" forState: UIControlStateNormal];
button.frame = CGRectMake( 0, 0, 100, 50 );
button.center = overlayController.view.center;
[overlayController.view addSubview: button];
// the controller's view is the first and only view in the
// new window. this ensures you get rotation events. Add any subviews
// that will appear in the new window to overlayContoller.view
[overlayWindow addSubview: overlayController.view];
[overlayWindow setWindowLevel: 1];
[overlayWindow makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[overlayController release];
[overlayWindow release];
[navigationController release];
[window release];
[super dealloc];
}
#end