UITabBarController rotation problem with popViewControllerAnimated and selectedIndex (iPhone SDK) - iphone

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].

Related

how to drag selected string in uitextview from out of the textview.?

*iam a beginner in iphone development.
i have one problem in uitextview...wat iam trying to do is dragging the the selected string in uitextview from out of the textview...and drag it to the tabbarcontroller is it possible plz any on help me on this....*this is the code ihave return up to now....plz help me
#
import <UIKit/UIKit.h>
#import "TabViewController.h"
#class TabBarViewController;
#interface TabBarAppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
{
TabBarViewController *txtviewcontroller;
UITabBarController *tabbar;
NSArray *viewcontrollerarray;
}
#property(nonatomic,retain)NSArray *viewcontrollerarray;
#property(nonatomic,strong)UITabBarController *tabbar;
#property(nonatomic,retain)TabBarViewController *txtviewcontroller;
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) TabBarViewController *viewController;
#end
#import "TabBarAppDelegate.h"
#import "TabViewController.h"
#implementation TabBarAppDelegate
#synthesize txtviewcontroller,tabbar,viewcontrollerarray;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor=[UIColor whiteColor];
self.tabbar=[[UITabBarController alloc]init];
txtviewcontroller=[[TabBarViewController alloc]init];
tabbar.delegate=self;
viewcontrollerarray=[[NSArray alloc]initWithObjects:txtviewcontroller, nil];
self.tabbar.viewControllers=viewcontrollerarray;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[TabBarViewController alloc] initWithNibName:#"TabBarViewController_iPhone" bundle:nil];
} else {
self.viewController = [[TabBarViewController alloc] initWithNibName:#"TabBarViewController_iPad" bundle:nil];
}
self.window.rootViewController = self.tabbar;
[self.window makeKeyAndVisible];
return YES;
}
#import <UIKit/UIKit.h>
#interface TabBarViewController : UIViewController
{
UITextView *textview;
}
#property(nonatomic,retain)UITextView *textview;
#end
#import "TabViewController.h"
#import "TabBarAppDelegate.h"
#include <QuartzCore/CoreAnimation.h>
#interface TabBarViewController ()
#end
#implementation TabBarViewController
#synthesize textview;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title=#"firstname";
CGRect textViewFrame = CGRectMake(20.0f, 20.0f, 280.0f, 124.0f);
textview = [[UITextView alloc] initWithFrame:textViewFrame];
textview.backgroundColor=[UIColor clearColor];
textview.textColor=[UIColor blackColor];
textview.editable=NO;
NSString *filePath=[[NSBundle mainBundle]pathForResource:#"satyadetails" ofType:#"txt"];
NSString *contentString=[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
textview.text=contentString;
textview.layer.borderWidth = 3.0f;
textview.layer.borderColor = [[UIColor grayColor] CGColor];
textview.returnKeyType = UIReturnKeyDone;
[self.view addSubview:textview];
enter code here
}
Step 1. Get event when user touches inside textview (You can get that by UITextView's Delegate (startEditing delegate))
Step 2. Add a UILabel on ur view giving position where user touches in textview and giving text as that of textview and giving clear color as backgroundcolor. (Do this inside delegate of textview)
Step 3. Inside touches move of ur view change the positions of ur label dynamically according to touches.
Step 4. When user moves its touches till dropping textview delegate will be called check there if(textview==droppingtextview) then put draggingtextview.text=label.text. and remove label from superview.

Tab Bar controller in iPhone

I have added Two Tabs in my app, to load two view controller using these tabs
Tab1 : Home
Tab2 : Favourite
so I have written below code to achieve this
In app Delegate
AppDelegate.h
#class ViewController,FavViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#property (strong, nonatomic) FavViewController *favViewController;
#property (strong, nonatomic) UITabBarController *tabBarController;
#end
AppDelegate.m
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController;
#synthesize favViewController;
#synthesize tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
baseTabBarController = [[UITabBarController alloc]init];
baseTabBarController.delegate=self;
viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *homeView = [[UINavigationController alloc]initWithRootViewController:viewController];
favViewController = [[FavViewController alloc] initWithNibName:#"FavViewController" bundle:nil];
favViewController.title = #"My Favourite";
UINavigationController *favouriteView = [[UINavigationController alloc] initWithRootViewController:favViewController];
NSArray *controllers = [NSArray arrayWithObjects:homeView,favouriteView, nil];
baseTabBarController.viewControllers = controllers;
[self.window addSubview:baseTabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)selectedViewController
{
if (tabBarController.selectedIndex == 0) {
}else if (tabBarController.selectedIndex == 1) {
[(FavViewController *)[(UINavigationController*)selectedViewController topViewController] getData];
}else if (tabBarController.selectedIndex == 2) {
NSLog(#"2");
}
}
and here is the Result Screen I am getting
So Where I am having trouble in this..
If I switch to the next screen my First tab doesn't load First Screen (Home screen) instead just stay on current screen.
Let me try with example
There are four screens in my app let say A, B, C, D
I have added Tabs for A and C screens,those are available in whole app
(All screen).
Now if I'll start app and go A ->B->C -> D and press on Home Tab (A)
it doesn't load Screen A, instead just stay on Current screen
but good thing is if i do same process with another tab C (My
Favourite) it loads correct screen.
Edit: I have implemented didSelectViewController method as #sanjit suggested me but in that I not able distinguish, which tab is tapped this time?
I would be really appreciate!! if someone can point me on right direction
Use Tabbar delegate and using parameter instance of uiviewcontroller call poptorootviewcontroller method. Hope it may work for you. Try the following code
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
UINavigationController *navController = (UINavigationController*)tabBarController.selectedViewController;
[navController popToRootViewControllerAnimated:YES];
}
replace the code
[viewController setTabBarItem:tab1];
[favViewController setTabBarItem:tab2];
with
[[tabbarController tabBar] setItems:[NSArray arrayWithObjects:tab1,tab2, nil]];
that should resolve.
Viewcontroller.h
#property(nonatomic, retain) UITabBarItem *item1;
#property(nonatomic, retain) UITabBarItem *item2;
#property(nonatomic, retain) UITabBarItem *item3;
#property(nonatomic, retain) UITabBarItem *item4;
#property(nonatomic, retain) IBOutlet UITabBar *tabBar;
Viewcontroller.m
#synthesize item1,item2,item3,item4,tabBar;
- (void)viewDidLoad
{
item1 = [[UITabBarItem alloc] initWithTitle:#"Home" image:[UIImage imageNamed:#"home.png"] tag:0];
item2 = [[UITabBarItem alloc] initWithTitle:#"My Lists" image:[UIImage imageNamed:#"mylist"] tag:1];
item3 = [[UITabBarItem alloc] initWithTitle:#"Search" image:[UIImage imageNamed:#"search.png"] tag:2];
item4 = [[UITabBarItem alloc] initWithTitle:#"Settings" image:[UIImage imageNamed:#"setting.png"] tag:3];
NSArray *items = [NSArray arrayWithObjects:item1,item2,item3,item4, nil];
[tabBar setItems:items animated:NO];
[tabBar setSelectedItem:[tabBar.items objectAtIndex:2]];
tabBar.delegate=self;
[self.view addSubview:tabBar
];
}
/****Function Call******/
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if(item.tag == 0)
{
Newsfeeds *obj = [[Newsfeeds alloc]initWithNibName:#"Newsfeeds" bundle:Nil];
[self.navigationController pushViewController:obj animated:NO];
}
else if(item.tag == 1)
{
MyLists *obj = [[MyLists alloc]initWithNibName:#"MyLists" bundle:Nil];
[self.navigationController pushViewController:obj animated:NO];
}
else if(item.tag == 3)
{
Settings *obj = [[Settings alloc]initWithNibName:#"Settings" bundle:Nil];
[self.navigationController pushViewController:obj animated:NO];
}
}

Tab Bar view does not load

In my app delegate interface I have:
#interface pivcalc1AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
IBOutlet UITabBarController *RootController;
}
#property (nonatomic, retain) IBOutlet UIWindow *Window;
#property (nonatomic, retain) IBOutlet UITabBarController *RootController;
in implementation, I have:
#synthesize Window;
#synthesize RootController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
[Window addSubview:RootController.view];
// Override point for customization after application launch.
[self.Window makeKeyAndVisible];
return YES;
}
In my main xib window I have a tab bar controller which is connected to app delegate as rootController.
When I run the program, the window shows but the tab bar view does not get loaded.
Appreciate any help. Thanks.
Are you have a subViewController in RootController in xib? if you're not, It's code following.
UITabBarController should be have a subViewController.
UINavigationController *localNavigationController;
NSMutableArray *localViewControllerArray = [[NSMutableArray alloc] initWithCapacity:2];
SubViewController *subviewController0 = [[SubViewController alloc] init];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:subviewController0];
[localViewControllerArray addObject:localNavigationController];
[subviewController0 release];
[localNavigationController release];
SubViewController *subviewController1 = [[SubViewController alloc] init];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:subviewController1];
[localViewControllerArray addObject:localNavigationController];
[subviewController1 release];
[localNavigationController release];
RootController.viewControllers = localViewControllerArray;
[localViewControllerArray release];
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];

iOS (iPhone/iPad) SDK - Application not changing orientation

In my custom Tab Bar Application, the orientation never seems to change, even if I force rotate the status bar. Here is my code in my AppDelegate:
AppDelegate.h:
#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
#class exampleViewContoller;
#class example1ViewController;
#class example2ViewController;
#class example3ViewController;
#class example4ViewController;
#interface <appname>AppDelegate : NSObject <UIApplicationDelegate, MBProgressHUDDelegate> {
UIWindow *window;
UITabBarController *rootController;
exampleViewContoller *viewController;
example1ViewController *viewController1;
example2ViewController *viewController2;
example3ViewController *viewController3;
example4ViewController *viewController4;
NSMutableData *responseData;
NSMutableArray *tweets;
MBProgressHUD *HUD;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *rootController;
#property (nonatomic, retain) IBOutlet exampleViewContoller *viewController;
#property (nonatomic, retain) IBOutlet example1ViewController *viewController1;
#property (nonatomic, retain) IBOutlet example2ViewController *viewController2;
#property (nonatomic, retain) IBOutlet example3ViewController *viewController3;
#property (nonatomic, retain) IBOutlet example4ViewController *viewController4;
#property (nonatomic, retain) NSMutableArray *tweets;
#end
AppDelegate.m:
#import "<appname>AppDelegate.h"
#import "exampleViewContoller.h"
#import "example1ViewController.h"
#import "example2ViewController.h"
#import "example3ViewController.h"
#import "example4ViewController.h"
#import "SBJson.h"
#define TMP NSTemporaryDirectory()
#implementation <appname>AppDelegate
#synthesize window = _window;
#synthesize rootController;
#synthesize viewController;
#synthesize viewController1;
#synthesize viewController2;
#synthesize viewController3;
#synthesize viewController4;
#synthesize tweets;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGFloat width = self.rootController.view.bounds.size.width;
CGFloat height = self.rootController.view.bounds.size.height;
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, width, height)];
UIImage *imageView = [UIImage imageNamed:#"TabBarBackground.png"];
UIColor *kMainColor = [[UIColor alloc] initWithPatternImage:imageView];
[v setBackgroundColor:kMainColor];
[kMainColor release];
[self.rootController.tabBar insertSubview:v atIndex:0];
[imageView release];
[v release];
responseData = [[NSMutableData data] retain];
tweets = [NSMutableArray array];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:#"http://api.twitter.com/1/statuses/user_timeline.json?screen_name=ENTER_USER_HERE&count=20"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSAssert(nil != self.rootController, #"tab bar controller not hooked up!");
BOOL iPad = NO;
#ifdef UI_USER_INTERFACE_IDIOM
iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#endif
if (iPad) {
self.viewController = [[[exampleViewContoller alloc] initWithNibName:#"exampleViewController_iPad" bundle:nil] autorelease];
self.viewController1 = [[example1ViewController alloc] initWithNibName:#"example1ViewController_iPad" bundle:nil] autorelease];
self.viewController2 = [[[example2ViewController alloc] initWithNibName:#"example2ViewController_iPad" bundle:nil] autorelease];
self.viewController3 = [[[example3ViewController alloc] initWithNibName:#"example3ViewController_iPad" bundle:nil] autorelease];
self.viewController4 = [[[example4ViewController alloc] initWithNibName:#"example4ViewController_iPad" bundle:nil] autorelease];
} else {
self.viewController = [[[exampleViewContoller alloc] initWithNibName:#"exampleViewContoller_iPhone" bundle:nil] autorelease];
self.viewController1 = [[[example1ViewController alloc] initWithNibName:#"example1ViewController_iPhone" bundle:nil] autorelease];
self.viewController2 = [[[example2ViewController alloc] initWithNibName:#"example2ViewController2_iPhone" bundle:nil] autorelease];
self.viewController3 = [[[example3ViewController alloc] initWithNibName:#"example3ViewController_iPhone" bundle:nil] autorelease];
self.viewController4 = [[[example4ViewController alloc] initWithNibName:#"example4ViewController_iPhone" bundle:nil] autorelease];
}
self.rootController.viewControllers = [NSArray arrayWithObjects:self.viewController, self.viewController4, self.viewController1, self.viewController3, self.viewController2, nil];
[viewController release];
[viewController1 release];
[viewController2 release];
[viewController3 release];
[viewController4 release];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
self.window.rootViewController = self.rootController;
#else
[self.window addSubview:rootController.view];
#endif
[self.window makeKeyAndVisible];
HUD = [[MBProgressHUD alloc] initWithView:viewController.view];
[viewController.view addSubview:HUD];
[HUD show:NO];
HUD.delegate = self;
HUD.labelText = #"Loading";
return YES;
}
//[---CODE CLIP---]
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation {
CGFloat width = self.rootController.view.bounds.size.width*2;
CGFloat height = self.rootController.view.bounds.size.height;
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, width, height)];
UIImage *imageView = [UIImage imageNamed:#"TabBarBackground.png"];
UIColor *kMainColor = [[UIColor alloc] initWithPatternImage:imageView];
[v setBackgroundColor:kMainColor];
[kMainColor release];
[self.rootController.tabBar insertSubview:v atIndex:0];
[imageView release];
[v release];
}
- (void)hudWasHidden {
[HUD removeFromSuperview];
}
//[---CODE CLIP---]
- (void)dealloc
{
[_window release];
[rootController release];
[HUD release];
[super dealloc];
}
#end
The problem is that when I rotate the device in iOS Simulator, the application won't rotate. Any ideas would be much appreciated!
UPDATE
I have also noticed that the launch image is also not rotating (for iPad that is - iPhone doesn't do landscape launch images).
NOTE FOR JMANS
I overrode UITabBarController:
#implementation UITabBarController (MyApp)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if(self.selectedIndex==4)
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
else
return (toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
I would start by including this in all of your view controllers:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
Also, make sure that you are supporting multiple orientations in your Info.plist.
Tab Bar Controllers and View Rotation
Tab bar controllers support a portrait orientation by default and do not rotate to a landscape orientation unless all of the contained view controllers support such an orientation. When a device orientation change occurs, the tab bar controller queries its array of view controllers. If any one of them does not support the orientation, the tab bar controller does not change its orientation.

iPhone SDK: Have UIView over camera display

How can I have a UIView over a view that contains a live view from the camera?
Put a NavigationController in uour MainWindow.xib, with a first Controller pointing to a custom CameraController (class attribute). Don't specify any XIB.
Keep a reference to this navigationController with an IBOutlet into your appDelegate, then call at laucnch :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
Create another xib for the overlay, with its controller, let's call it OverlayViewController.
Then into this CameraViewController :
.h
#interface CameraController : UIViewController {
UIImagePickerController* __picker;
OverlayViewController* __overlay;
}
#property (nonatomic, retain) UIImagePickerController* picker;
#property (nonatomic, retain) OverlayViewController* overlay;
.m
- (void) viewDidLoad {
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
self.picker.showsCameraControls = NO;
self.picker.navigationBarHidden = YES;
self.picker.wantsFullScreenLayout = YES;
// Insert the overlay
self.overlay = [[OverlayViewController alloc] initWithNibName:#"Overlay" bundle:nil];
self.overlay.pickerRef = self.picker;
self.picker.cameraOverlayView = self.overlay.view;
[self presentModalViewController:self.picker animated:NO];
}