if I run my app under iOS5 all seems to be fine. But if I try to load a view with a TabBar and some views in it.
The screen looks actually like the picture below, so a black tabbar and the first subview of the tabbar which should be onscreen is deallocated. I assume, that the binding between the view and the tabbar does not exist.
So I think it has something to do how I link the views with the tabbar.
It works like that:
NSMutableArray* controllersForHome = [[self.tabBarController.viewControllers mutableCopy] autorelease];
[controllersForHome insertObject:somveViewController atIndex:[controllersForHome count]];
[self.tabBarController setViewControllers:controllersForHome];
[someViewController release];
I've read that inserting the object at an index >0 should be helpful, but unfortunately not in my case. Still the same issue.
Has somebody a guess and can give me a hint how to solve that problem?
Thanks,
Andreas
this is how I solved the problem in iOS 5:
Instead of using this:
SMutableArray* controllersForHome = [[self.tabBarController.viewControllers mutableCopy] autorelease];
[controllersForHome insertObject:someViewController atIndex:[controllersForHome count]];
[self.tabBarController setViewControllers:controllersForHome];
[someViewController release];
You should use this:
self.tabBarController.viewControllers = [[NSArray arrayWithArray:self.tabBarController.viewControllers] arrayByAddingObject:someViewController];
Related
I am taking tab bar controller on view controller rather than delegate, and used code what mentioned below.
tabController = [[UITabBarController alloc]init];
tabController.delegate = self;
scanView = [[ScanTicketView alloc] init];
searchView = [[SearchView alloc] init];
historyView = [[HistoryView alloc]init];
tabController.viewControllers=[NSArray arrayWithObjects:scanView,searchView,historyView, nil];
[self.navigationController pushViewController:tabController animated:YES];
It works, but now how to apply images to these views. Can anyone help me here.
Thanks in advance.
You can use:
viewController.tabBarItem.image = [UIImage imageNamed:#"imageName.png"];
if you want to apply the image only to selected view controller in Tab bar, then UITabBarController has a selectedViewController and a selectedIndex property also see tabBarController.tabBar.selectedItem.tag if this may help.
Tabbar controller has its own property of Image and Title.
Go through this UITabbar Tutorial to understand properly. You can set image through property window or even through coding also.
Best Luck !
I'm having a really strange issue. I've written an app with five tabs in a UITabBar. When I set the TabBarController's viewControllers property, I set it with five UINavigationControllers, so that each tab will have a UINavigationController within it.
Four of the tabs have it working perfectly. The navigation bar is there when I launch and switch to that tab. However, one of the tabs does not contain the UINavigationBar as I expected it to, and I can't understand why, because I initialized it exactly the same way I initialized all the others.
Here's some sample code from the AppDelegate.m file of initializing the individual view controllers:
SpotFilterViewController *spotList = [[SpotFilterViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:spotList];
[tabs addObject:navigationController];
[navigationController release];
[spotList release];
MySpotViewController *mySpot = [[MySpotViewController alloc] initWithSpot:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:mySpot];
[tabs addObject:mySpot];
[navigationController release];
[mySpot release];
Note: navigationController was declared above.
Anyone else run into this problem before? Or anyone have any idea why this might be happening? Any help is much appreciated. Thanks!
The problem is that you are doing this:
[tabs addObject:mySpot];
instead of this:
[tabs addObject:navigationController];
I am trying to add UITabBarItems to a UITabBar not to a tabbar controller. Here is what I tried to do. It is always crashing when I am calling setItems. Can any please point out whats wrong.
My_Accounts *my_AccountsVC = [[My_Accounts alloc] init];
Payments *paymentsVC = [[Payments alloc] init];
Transfer *transferVC = [[Transfer alloc] init];
NSArray *VCArray = [[NSArray alloc] initWithObjects:my_AccountsVC,paymentsVC,transferVC, nil];
[self.tabbar setItems:VCArray];
Thanks
If you look at items, it takes an array of UITabBarItems and not UIViewController subclasses which you seem to be passing.
You will have to keep track of the view controllers elsewhere and pass an array of UITabBarItems and handle the view controllers in the UITabBar's delegate.
Or much better, use UITabBarController.
I believe you are misunderstanding how a UITabBarController works (documentation link). You must add the UIViewControllers to the UITabBarController using the viewControllers property.
The last line you have should read:
[tabBarController setViewControllers:VCArray];
The tabBar property of the UITabBarController is read-only. You cannot set that.
If you have a UITabBar (documentation link) without a UITabBarViewController, then you will need to use the method:
- (void)setItems:(NSArray *)items animated:(BOOL)animated
However, these items are not UIViewControllers! They are instances of UITabBarItem (documentation link). You may set these all at once by putting them into an array, or you can set them per view controller. There are several system items you may use (More, Favorites, etc) or you may use – initWithTitle:image:tag: to create a custom item.
Code seems wrong. I guess
[self.tabbar setItems:VCArray];
Above line should have parameter of Array of UITabBarItems. You passed items of UIViewController I guess. You should Create UITabbarItems and pass array of that in setItems method.
You should do something like below:
UITabBarItem *tabOne = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:0];
UITabBarItem *tabTwo = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
NSArray *arrTabbarItems = [NSArray arrayWithObjects:tabOne,tabTwo, nil];
[tabbar setItems:arrTabbarItems];
I am not sure what it will do as I am always using UITabBarController. Hope this help.
I have a view that is doing a lot of functions and when I get to the point that I am done I want to change to a newViewcontroller. if I where to do this from the rootview I just call.
NewPageViewController *newDetailViewController = [[NewPageViewController alloc] initWithNibName:#"NewPageViewController" bundle:nil];
detailViewController = newDetailViewController;
But I need to do it from my old detail (the right side)
I am downloading a file in a splitview iPad app from the right side and after the file is downloaded I need to in my method change the right side of the splitview to a new nib file so I can open and edit the file
Can someone point me in the right way.
Now I have :
-(void)changeView {
ListController *newDetailViewController = [[ListController alloc] initWithNibName:#"ListController"bundle:nil]
NSArray *viewControllers = [NSArray arrayWithObjects:[splitViewController.viewControllers objectAtIndex:0], newDetailViewController, nil];
splitViewController.viewControllers = viewControllers;
[viewControllers release];
}
-(void)downloadfile {
//I do all my work and get the file.
NSLog(#"I need to change views now.");
[self changeView];
}
I don't get any errors but the right side view is not changing.
As of iOS8 you can use the -showDetailViewController:sender: method on the UISplitViewController. See the Apple docs on UISplitViewController.
There is an NSArray *viewControllers property on the UISplitViewController class. The first item in this array is your master VC, the second in the detail VC. Re-assign this property to a new array containing the same master VC but a new details VC:
// don't forget to set the delegate of myNewDetailViewController appropriately!
myNewDetailViewController.delegate = ...
NSArray newVCs = [NSArray arrayWithObjects:[uiSplitVC.viewControllers objectAtIndex:0], myNewDetailViewController, nil];
uiSplitVC.viewControllers = newVCs;
API ref for UISplitViewController: http://developer.apple.com/library/ios/#documentation/uikit/reference/UISplitViewController_class/Reference/Reference.html
N.B: do not try replacing the master VC -- it usually goes horribly wrong somehow. I tried many many ways of replacing the master, it always went wrong in some very annoying way. Replacing the detail VC is fine though!
As #chris mentioned you can the use Delegate of UISplitViewController for iOS 8 and above which is the best possible way.
-(void)showDetailViewController:(UIViewController *)vc sender:(nullable id)sender NS_AVAILABLE_IOS(8_0);
I have a very strange problem with UINavigationController on the iphone and I am banging my head against the wall on this.
Gist of it is I am executing a call to a server and when that call fails I swap out the current view with a view containing an error message. The code in question is called on the main thread by using performSelectorOnMainThread
What happens in practice is that on the device it shows a blank white screen about half the time. On the simulator it presents a blank screen every time leading me to think this is perhaps some kind of timing problem that is more prominent due to better processing speeds in a simulator. This works perfectly if I call the same function by clicking a button in the ui to display the page so I don't think its a problem with the code itself.
I have verified that the controller I am adding is in the navigation stack. Verified it is being called on the main thread, it is visible, the frame size and location are correct. I have tried explicitly setting the view to be visible, moved it to the front in its parent view and called setNeedsDisplay and even manually called drawRect. None of this works.
Any thoughts on what could be going on here? I am assuming it has something to do with the run loop but I can't figure it out. Help would be much appreciated. The relatively simple code in question is below
UINavigationController* navController = self.navigationController;
int count = [navController.viewControllers count];
NSMutableArray* controllers = [[NSMutableArray alloc] initWithCapacity:count];
for (int i=0; i<count; i++) {
if (self == [self.navigationController.viewControllers objectAtIndex:i]) {
[controllers addObject:newController];
}
else {
[controllers addObject:[self.navigationController.viewControllers objectAtIndex:i]];
}
}
[self.navigationController setViewControllers:controllers animated:YES];
[controllers release];
I really don't understand what you're doing there. Something like this won't work?
- (void)displayMyErrorVC {
MyErrorVC *errorVC = [[[MyErrorVC alloc] init] autorelease];
[self.navigationController pushViewController:errorVC animated:YES];
}
And then in your other thread, if you have an error:
[self performSelectorOnMainThread:#selector(displayMyErrorVC) withObject:nil waitUntilDone:NO];