I've got a UIButton that I'd like to display in the same position in front of all of the viewControllers of my UINavigationController, much in the same way that a UINavigationBar would - but in this case, I'd like the button to float in the lower-left corner of the screen, like so:
I could try manually adding it to each viewController, but I'd prefer to do it once and forget about it.
Adding the button as a subview to the UIWindow doesn't seem to work - it winds up behind the rootViewController.
Update: I've figured out a way to do it, but because it involves iOS 7, I can't post the solution here until the NDA is lifted. The good news is, it's fairly simple to accomplish this! I'll post something once it's OK to share.
create a NSObject class buttonPanel and allocate the button and you can call the button panel as follow:
buttonPanel * buttonPanel = [[buttonPanel alloc] init];
buttonPanel.delegate = self;
[buttonPanel showButtonWithNavigationController:self.navigationController inView:self.parentViewController.view];
ButtonPanel class:
//ButtonPanel.h
#import <Foundation/Foundation.h>
#class ButtonPanel;
#protocol ButtonPanelDelegate <NSObject>
-(void) ButtonPanel:(ButtonPanel*) buttonPanel ;
#end
#interface ChatMessagePanel : NSObject<UIActionSheetDelegate> {
UINavigationController *navigationController;
id<ButtonPanelDelegate> delegate;
}
#property(nonatomic,retain) UINavigationController *navigationController;
#property (nonatomic,retain) id<ButtonPanelDelegate> delegate;
-(void)showButtonWithNavigationController:(UINavigationController*) navigationController inView:(UIView*) view;
#end
// ButtonPanel.m
#import "ChatMessagePanel.h"
#implementation ButtonPanel
#synthesize userListModal,delegate,navigationController;
-(void)showMessageWithNavigationController:(UINavigationController*) _navigationController inView:(UIView*) view
{
self.navigationController = _navigationController;
baseViewWidth = view.frame.size.width;
baseViewHeight = view.frame.size.height;
incomingRequestActionSheet = [[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
[incomingRequestActionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[incomingRequestActionSheet showInView:view];
UIButton *blockUserBttn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
[blockUserBttn setTitle:NSLocalizedString(#"BLOCK_USERS",#"") forState:UIControlStateNormal];
[blockUserBttn setBackgroundColor:[UIColor clearColor]];
[blockUserBttn addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *blockUserBarBttn = [[UIBarButtonItem alloc] initWithCustomView:blockUserBttn];
[optionToolBar setItems:[NSArray arrayWithObjects:blockUserBarBttn, nil]];
[incomingRequestActionSheet addSubview:optionToolBar];
}
-(void)buttonAction:(id) sender
{
}
#end
I don't think this is possible in the ios. You must have to do
individual for all view.
You can simply add the UIButton on the window like
[self.window addSubview:Button];
after self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
Related
I've a UITableView with some static cells in it. One of them shows a UIActionSheet when touched. Here goes the code for it:
viewDidLoad: initialize controls.
- (void)viewDidLoad
{
[super viewDidLoad];
[self initializeControls];
}
initializeControls: add gesture recognizer to the label within the cell in order to show the UIActionSheet.
- (void)initializeControls {
[...]
// Places Label
self.placeNamesLabel.userInteractionEnabled = YES;
tapGesture = \
[[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(didPlaceNamesLabelWithGesture:)];
[self.placeNamesLabel addGestureRecognizer:tapGesture];
tapGesture = nil;
[...]
}
didPlaceNamesLabelWithGesture: initialize UIActionSheet, add a UITableView from StoryBoard and a Close button.
- (void)didPlaceNamesLabelWithGesture:(UITapGestureRecognizer *)tapGesture
{
// Show UITableView in UIActionView for selecting Place objects.
placesActionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[placesActionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect tableFrame = CGRectMake(0, 40, 0, 0);
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
AddArticlesPlacesViewController *placesView = [storyBoard instantiateViewControllerWithIdentifier:#"PlacesForArticle"];
placesView.managedObjectContext = self.managedObjectContext;
placesView.view.frame = tableFrame;
[placesActionSheet addSubview:placesView.view];
placesView = nil;
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Close"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissPlacesActionSheet:) forControlEvents:UIControlEventValueChanged];
[placesActionSheet addSubview:closeButton];
closeButton = nil;
[placesActionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[placesActionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
dismissPlacesActionSheet: close UIActionSheet.
-(void)dismissPlacesActionSheet:(id)sender {
[placesActionSheet dismissWithClickedButtonIndex:0 animated:YES]; }
Everything works and the UITableView populates fine from Core Data. So where is the problem? The point is that when any row is tapped, the entire table gets empty (I can't post images yet, sorry).
I've tried to add a new custom row with a button that fires a push segue to the AddArticlesPlacesViewController; this works fine and the behavior is as expected. So the problem is between UIActionSheet and UITableView.
Here are the interfaces for both view controllers:
Main.
#interface AddArticleViewController : UITableViewController <NSFetchedResultsControllerDelegate, [...]>
[...]
#property (weak, nonatomic) IBOutlet UILabel *placeNamesLabel;
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
[...]
#end
Secondary.
#interface AddArticlesPlacesViewController : UITableViewController <NSFetchedResultsControllerDelegate>
#property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#end
I'm stuck. What am I doing wrong?
Regards.
Pedro Ventura.
you can use tableview's delegate method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
NSLog(#"indexpath-->%d",indexPath.row);
if (indexPath.row==0 || indexPath.row == 2 )
{
NSLog(#"row %d",indexPath.row);
}
if (indexPath.row==1)
{
actionsheet=[[UIActionSheet alloc]initWithTitle:#"Sample" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil, nil];
[actionsheet showInView:self.view];
}
}
And you can use UIActionsheetDelegate's method for Selected Button:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"index==>%d",buttonIndex);
NSLog(#"cancel");
}
edit 3
Good news and bad news. The good news is that in the Connections Inspector by disconnecting the overlay UIToolbar and connecting the UIImageview, I see theKing, but then -- the bad news -- I do not see the UIToolbar, which I also need. So the question now is, how can the user get back to the calling VC when s/he is finished here? How can both the toolbar and the image be part of the the overlay, or can the "go back button" toolbar be on the non-overlay view, or something? Or how can I make both the toolbar and the image show on the OverlayViewController?
edit 3
edit 2
setupImagePicker in OverlayViewController.m.
- (void)setupImagePicker:(UIImagePickerControllerSourceType)sourceType
{
self.imagePickerController.sourceType = sourceType;
if (sourceType == UIImagePickerControllerSourceTypeCamera)
{
// user wants to use the camera interface
//
self.imagePickerController.showsCameraControls = NO;
if ([[self.imagePickerController.cameraOverlayView subviews] count] == 0)
{
// setup our custom overlay view for the camera
//
// ensure that our custom view's frame fits within the parent frame
CGRect overlayViewFrame = self.imagePickerController.cameraOverlayView.frame;
CGRect newFrame = CGRectMake(0.0,
CGRectGetHeight(overlayViewFrame) -
self.view.frame.size.height - 10.0,
CGRectGetWidth(overlayViewFrame),
self.view.frame.size.height + 10.0);
self.view.frame = newFrame;
[self.imagePickerController.cameraOverlayView addSubview:self.view];
}
}
}
edit 2
edit 1
This is the PhotoPicker sample code link.
edit 1
edit 0
Has anyone tried this on an iPhone instead of an iPad? I do not have an iPhone, but I was reading Matt Neuburg's book today where he says UIImagePicker works differently on the 2 devices.
edit 0
I cannot see the image I am attempting to overlay across the camera view in UIImagePicker. No matter how I add the IBOutlet, as a property or not, the image does not show, but the overlayed toolbar shows fine. Why?
OverlayViewController.h with theKing IBOutlet added to Apple's sample code and then commented out for now.
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioServices.h>
#protocol OverlayViewControllerDelegate;
#interface OverlayViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
// IBOutlet UIImageView* theKing; ******** temporarily commented out
id <OverlayViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <OverlayViewControllerDelegate> delegate;
#property (nonatomic, retain) UIImagePickerController *imagePickerController;
- (void)setupImagePicker:(UIImagePickerControllerSourceType)sourceType;
#end
#protocol OverlayViewControllerDelegate
- (void)didTakePicture:(UIImage *)picture;
- (void)didFinishWithCamera;
#end
OverlayViewController.m with the property for theKing IBOutlet added to Apple's sample code.
#import "OverlayViewController.h"
enum
{
kOneShot, // user wants to take a delayed single shot
kRepeatingShot // user wants to take repeating shots
};
#interface OverlayViewController ( )
#property (assign) SystemSoundID tickSound;
#property (nonatomic, retain) IBOutlet UIImageView* theKing; // added *********
#property (nonatomic, retain) IBOutlet UIBarButtonItem *takePictureButton;
#property (nonatomic, retain) IBOutlet UIBarButtonItem *startStopButton;
#property (nonatomic, retain) IBOutlet UIBarButtonItem *timedButton;
#property (nonatomic, retain) IBOutlet UIBarButtonItem *cancelButton;
#property (nonatomic, retain) NSTimer *tickTimer;
#property (nonatomic, retain) NSTimer *cameraTimer;
I cannot see the image theKing when I execute the code on an iPhone. Below is a view of the nib I have added showing some of the connections made. No errors are thrown, but I cannot see the image, only the UIToolbar already added.
Update
I've updated it now and tried to replicate what you've done in your question. I've included full source of .h/.m and theKing#2x.png. Create a new project and paste the source into the files and add theKing#2x.png. Everything is done programmatically - you don't need to set anything up in interface builder other than embedding your view in a navigation controller.
Here is theKing#2x.png - http://i.imgur.com/0DrM7si.png
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// assign action to button
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(0, 0, 200, 60);
myButton.center = self.view.center;
[myButton addTarget:self action:#selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
[myButton setTitle:#"Image Picker" forState:UIControlStateNormal];
[self.view addSubview:myButton];
}
- (void)buttonPress:(id)sender {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// alert the user that the camera can't be accessed
UIAlertView *noCameraAlert = [[UIAlertView alloc] initWithTitle:#"No Camera" message:#"Unable to access the camera!" delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
[noCameraAlert show];
} else {
// prepare imagePicker view
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
imagePicker.allowsEditing = NO;
imagePicker.showsCameraControls = NO;
// create view for overlay
CGRect overlayRect = CGRectMake(0, 0, imagePicker.view.frame.size.width, imagePicker.view.frame.size.height);
UIView *overlayView = [[UIView alloc] initWithFrame:overlayRect];
// prepare the image to overlay
UIImageView *overlayImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"theKing"]];
overlayImage.center = overlayView.center;
overlayImage.alpha = 0.5;
// prepare toolbar for overlay
UIToolbar *overlayToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 600, overlayView.frame.size.width, 40)];
overlayToolbar.center = CGPointMake(overlayView.center.x, overlayView.frame.size.height - 20);
overlayToolbar.barStyle = UIBarStyleBlack;
UIBarButtonItem *takePictureButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(takePictureButtonPressed:)];
UIBarButtonItem *flexibleBarSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
flexibleBarSpace.width = 1000;
UIBarButtonItem *startStopButton = [[UIBarButtonItem alloc] initWithTitle:#"Snap" style:UIBarButtonItemStyleBordered target:self action:#selector(startStopButtonPressed:)];
UIBarButtonItem *timedButton = [[UIBarButtonItem alloc]initWithTitle:#"Timed" style:UIBarButtonItemStyleBordered target:self action: #selector(timedButtonPressed:)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action: #selector(cancelButtonPressed:)];
overlayToolbar.items = [NSArray arrayWithObjects:takePictureButton, flexibleBarSpace, startStopButton, timedButton, cancelButton, nil];
[overlayView addSubview:overlayImage];
[overlayView addSubview:overlayToolbar];
// add the image as the overlay
[imagePicker setCameraOverlayView:overlayView];
// display imagePicker
[self.navigationController presentViewController:imagePicker animated:YES completion:nil];
}
}
#pragma mark - UIBarButton Selectors
- (void)takePictureButtonPressed:(id)sender {
NSLog(#"takePictureButtonPressed...");
// TODO: take picture!
}
- (void)startStopButtonPressed:(id)sender {
NSLog(#"startStopButtonPressed...");
// TODO: make this do something
}
- (void)timedButtonPressed:(id)sender {
NSLog(#"timedButtonPressed...");
// TODO: implement timer before calling takePictureButtonPressed
}
- (void)cancelButtonPressed:(id)sender {
NSLog(#"cancelButtonPressed");
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UIImagePickerController Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)editingInfo {
// determine if the user selected or took a new photo
UIImage *selectedImage;
if ([editingInfo objectForKey:UIImagePickerControllerOriginalImage]) selectedImage = (UIImage *)[editingInfo objectForKey:UIImagePickerControllerOriginalImage];
else if ([editingInfo objectForKey:UIImagePickerControllerEditedImage]) selectedImage = (UIImage *)[editingInfo objectForKey:UIImagePickerControllerEditedImage];
// TODO: Do something with selectedImage (put it in a UIImageView
// dismiss the imagePicker
[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Screenshot
This is what it looks like when I run it.
Does this satisfy your app requirements?
One answer I have found is to put the image on a huge button which has as an action Done:. I'll have to figure out a way to tell my user to do it, but at least this will dismiss the image view and finish my setup mode. I would really like a cleaner alternative, so please add answers if you have them. Thanks to all who have considered my question.
I want is on main window to present the created modalViewController view when the infobutton is pressed. But when I press Info button on the main window nothing happens.
In the mainviewcontroller.h file I have following code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface imageviewViewController : UIViewController{
AVAudioPlayer *audioPlayer;
}
#property (nonatomic, retain) UIToolbar *toolbar;
#property (nonatomic, assign) UIBarButtonSystemItem currentSystemItem;
#property (nonatomic, retain) AVAudioPlayer *audioPlayer;
#end
In the mainviewcontroller.m file have following code:
#import "imageviewViewController.h"
#import "Infoviewcontroller.h"
#implementation imageviewViewController
#synthesize toolbar;
#synthesize currentSystemItem;
#synthesize audioPlayer;
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:nil
action:#selector(Infobuttonpressed)];
// flex item used to put space in between buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *toolbarItems = [NSArray arrayWithObjects: settingsButton, flexItem, systemItem, flexItem, systemItem1,flexItem, systemItem2, flexItem, systemItem3, flexItem, infoItem, nil];
[toolbar setItems:toolbarItems];
[settingsButton release];
[systemItem release];
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[infoItem release];
[flexItem release];
[super viewDidLoad];
}
- (void) Infobuttonpressed: (id) sender
{
Infoviewcontroller *myView = [[Infoviewcontroller alloc] init];
[self presentModalViewController:myView animated:YES]; // present view modally
[self.navigationController pushViewController:myView animated:YES]; // add to navigation stack
[myView release];
}
In the Infoviewcontroller.h file have following code:
#import <UIKit/UIKit.h>
#interface Infoviewcontroller : UIViewController <UITextViewDelegate>
{
UITextView *textView;
}
#property (nonatomic, retain) UITextView *textView;
#property (nonatomic, assign) UINavigationBar *navBar;
#end
Then in the infoviewcontroller.m file have the following code:
#import "Infoviewcontroller.h"
#implementation Infoviewcontroller
#synthesize textView;
#synthesize navBar;
-(void)dealloc
{
[textView release];
[navBar release];
[super dealloc];
}
-(void)setupTextView
{
self.textView = [[[UITextView alloc] initWithFrame:self.view.frame] autorelease];
self.textView.textColor = [UIColor redColor];
self.textView.font = [UIFont fontWithName:#"System Bold" size:13];
self.textView.delegate = self;
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.text = #"This is UITextView\nThis is UITextView\nThis is UITextView\nThis is UITextView";
[self.view addSubview: self.textView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
navBar = [[UINavigationBar alloc] init];
UINavigationItem *navItem = [[[UINavigationItem alloc] initWithTitle:#"ModalViewControllerTest"] autorelease];
UIBarButtonItem *done = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissView:)] autorelease];
navItem.rightBarButtonItem = done;
navBar.items = [NSArray arrayWithObject:navItem];
[self.view addSubview:navBar];
}
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:nil
action:#selector(Infobuttonpressed)];
Should be
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(Infobuttonpressed:)];
Spot the difference? First, you're missing a target (should be self, not nil). Second, the colon at the end of the selector is part of the signature, you were missing it so it was not calling your method (which is InfoButtonPressed:(id)sender).
As an aside, methods should start with a lower case name.
you dont need both pushModalViewController and pushViewController
Assuming your views are already controlled within an instance of a UINavigationController pushModalViewController on its own will work.
edit - remove the reference to pushViewController. Does it now work?
-(void) Infobuttonpressed: (id) sender;
{ Infoviewcontroller *myView = [[Infoviewcontroller alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myView];
UIViewController *pushController = [[UIViewController alloc] init];
[self addSubview:pushController.view];
[pushController presentModalViewController:navController animated:YES];
[myView release];
}
The target of your infoItem is set to nil. You should set this to an instance of the class where the Infobuttonpressed is defined. I assume that is the mainviewController?
Also, the selector to call is specified as: #selector(Infobuttonpressed), while the method is called "Infobuttonpressed:", that is, with a parameter. That will likely result in a runtime error once you fix the target.
Then, there are some peculiarities in the way you handle things. As Nik already answered, you need to choose whether you want to present the view modally (with presentModalViewController), or present it as part of the navigation stack (with pushViewController), you cannot do both.
If you present it as part of the navitation stack, then it already has a navigation bar, and so you should not add one yourself. Just set the properties of the self.navigationItem.
Have a look at the View Controller Programming Guide for more info.
In iOS, my views work individually but I can't switch between them.
Now after a lot of google-ing around I've fond that the navigation based app would work great with the stack for views. The problem is all my views are nib/xib-less and custom tailored in the source code. Because of that I need my own UINavigationController and push and pop views by hand/code. Since every tutorial is either nib/xib and IB bound or just a mash of code snippets I need a concrete example how to do it.
A simple example with 2 programmatically created view (can be empty just have to use loadView instead of initializing with a nib/xib) and a working stack (a push and a pop of the views like: load app,create some root view if needed, push one view and from that view push the second one and then pop them) would be awesome, or at least a tutorial in that way with the whole source of the project and not snippets.
Thanks in advance.
EDIT: After some extra thinking, a little more clarification wouldn't be bad. My app will basically consist of 5 or 6 views which will be called form their respective previous view, i.e. a drill-down app.
Here's a brief code, only the essential parts:
CodeViewsPushingAppDelegate.m
#import "CodeViewsPushingAppDelegate.h"
#import "ViewNumberOne.h"
#implementation CodeViewsPushingAppDelegate
#synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
UINavigationController *navController = [[UINavigationController alloc] init];
ViewNumberOne *view1 = [[ViewNumberOne alloc] init];
[navController pushViewController:view1 animated:NO];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
ViewNumberOne.h
#import <UIKit/UIKit.h>
#interface ViewNumberOne : UIViewController
{
UIButton *button;
}
- (void)pushAnotherView;
#end
ViewNumberOne.m
#import "ViewNumberOne.h"
#import "ViewNumberTwo.h"
#implementation ViewNumberOne
- (void)loadView
{
[super loadView];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(110, 190, 100, 20);
[button setTitle:#"Push Me!" forState:UIControlStateNormal];
[button addTarget:self action:#selector(pushAnotherView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)pushAnotherView;
{
ViewNumberTwo *view2 = [[ViewNumberTwo alloc] init];
[self.navigationController pushViewController:view2 animated:YES];
[view2 release];
}
ViewNumberTwo.h
#import <UIKit/UIKit.h>
#interface ViewNumberTwo : UIViewController
{
UILabel *label;
}
#end
ViewNumberTwo.m
#import "ViewNumberTwo.h"
#implementation ViewNumberTwo
- (void)loadView
{
[super loadView];
label = [[UILabel alloc] init];
label.text = #"I am a label! This is view #2";
label.numberOfLines = 2;
label.textAlignment = UITextAlignmentCenter;
label.frame = CGRectMake(50, 50, 200, 200); //whatever
[self.view addSubview:label];
}
This is a very important auto rotate issue and easy to reproduce.
My application has a UITabBarController. Each tab is a UINavigationController. Auto rotation is handled with normal calls to shouldAutorotateToInterfaceOrientation and didRotateFromInterfaceOrientation.
The interface rotates normally until I call UIViewController.popViewControllerAnimated and change UITabBarController.selectedIndex.
Steps to reproduce:
Create a demo Tab Bar Application.
Add the following code to the App Delegate .h file:#import <UIKit/UIKit.h>
#interface TestRotationAppDelegate : NSObject {
UIWindow *window;
UITabBarController *tabBarController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
// Redefine the interface to cach rotation messages
#interface UITabBarController (TestRotation1AppDelegate)
#end
Add the following code to the App Delegate .m file:#import "TestRotationAppDelegate.h"
#implementation TestRotationAppDelegate
#synthesize window;
#synthesize tabBarController;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
return YES;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(void)dealloc {
[tabBarController release];
[window release];
[super dealloc];
}
#end
#implementation UITabBarController (TestRotation1AppDelegate)
-(void)viewDidLoad {
[super viewDidLoad];
// Add a third tab and push a view
UIViewController *view1 = [[[UIViewController alloc] init] autorelease];
view1.title = #"Third";
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:view1] autorelease];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObjectsFromArray:self.viewControllers];
[array addObject:nav];
self.viewControllers = array;
// Push view2 inside the third tab
UIViewController *view2 = [[[UIViewController alloc] init] autorelease];
[nav pushViewController:view2 animated:YES];
// Create a button to pop view2
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(50, 50, 220, 38);
[button setTitle:#"Pop this view" forState:UIControlStateNormal];
[button addTarget:self action:#selector(doAction) forControlEvents:UIControlEventTouchUpInside];
[view2.view addSubview:button];
}
-(void) doAction {
// ROTATION PROBLEM BEGINS HERE
// Remove one line of code and the problem doesn't occur.
[self.selectedViewController popViewControllerAnimated:YES];
self.selectedIndex = 0;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
#end
The interface auto rotates normally until you tap the button on tab #3.
Your help will be geatly appreciated!
iPhone SDK 3.2 solves this issue.
With previous SDK use [self.selectedViewController popViewControllerAnimated:NO].