I have 6 view controllers when my app starts. It's like an image gallery. When a user pushes for example the button on the third view, he/she should gets to the third view in the tab bar.
I use this code to launch the view controllers on the top of the tab bar controller:
- (void)viewDidAppear:(BOOL)animated {
static BOOL first = YES;
if (first) {
UIViewController *popup = [[Home1ViewController alloc] initWithNibName:#"Home1ViewController" bundle:nil];
[self presentViewController:popup animated:NO completion:nil];
first = NO;
}
}
By using this code to dismiss this new view, I'm just coming to the specific view, but not my tab-bar page...
-(IBAction)dismissView {
TabBarPage3 *screen = [[ TabBarPage3 alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:YES];
}
Please help me with this!
Thanks
This is the code for change view on TabBar
[((UITabBarController *)(self.parentViewController))setSelectedIndex:index];
Sample Project of UITabbar
Related
in my app i am trying to make a slider using the LWSlideShow LWSlideShow
the source of images are fetched from my server after trying the solution here i stuck with error that said unbalanced call and it means that i am presenting a modal view on a view that did not completed his animation after solving this problem by putting animation to no the splashView that i present will be dismissed before the images are downloaded here is my code for further explanation:
- (IBAction)goDownload {
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"Splash"];
[self.navigationController presentViewController:vc animated:YES completion:nil];
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableArray *array = [#[] mutableCopy];
LWSlideItem *item = [LWSlideItem itemWithCaption:#""
imageUrl:#"http://code-bee.net/geeks/images/cover-1.jpg"];
[array addObject:item];
item = [LWSlideItem itemWithCaption:#""
imageUrl:#"http://code-bee.net/geeks/images/cover-2.jpg"];
[array addObject:item];
item = [LWSlideItem itemWithCaption:#""
imageUrl:#"http://code-bee.net/geeks/images/cover-3.jpg"];
[array addObject:item];
LWSlideShow *slideShow = [[LWSlideShow alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 120)];
slideShow.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//slideShow.delegate = self;
[self.view addSubview:slideShow];
slideShow.slideItems = array;
if ([slideShow.slideItems count] == [array count]) {
[self dismissViewControllerAnimated:YES completion:nil];
}
});
}
//
//-(void)viewWillAppear:(BOOL)animated
//{
//
// UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"Splash"];
// [self.navigationController presentViewController:vc animated:YES completion:nil];
//}
- (void)viewDidLoad {
[super viewDidLoad];
[self goDownload];
}
also you can see from the code that also i try to use viewWillAppear same thing happened what i want is when the images are downloaded the splashView need to be dismissed i dont know what i am doing wrong
Running that code from a VC anytime before viewDidAppear (like viewDidLoad, viewWillAppear) will cause the problem you describe. But you probably don't want the slide show view to appear - even for an instant - until you're done fetching the assets. This is a common problem.
The solution is to realize that the "splash screen" and the network tasks aren't just preamble, they are as much a part of your application as the slide show.
EDIT
Make that Splash vc the app's initial view controller in storyboard. Right now, the slide show vc probably looks like this:
Uncheck the "Is Initial View Controller" checkbox, find your splash view controller (in the same storyboard, I hope) and check it's box to be the initial view controller. Now your app will start up on the splash vc, like you want it.
When the splash vc done, it can present the slide show vc, or it can even replace itself (with the slide show ) as the app window's root.
To replace the UI, I use variations of this snippet...
// in the splash vc, after all of the asset loading is complete
// give what used to be your initial view controller a storyboard id
// like #"MySlideShowUI"
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"MySlideShowUI"];
UIWindow *window = [UIApplication sharedApplication].delegate.window;
window.rootViewController = vc;
[UIView transitionWithView:window
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:nil
completion:nil];
For my code structure:
On AppDelegate, I declared 4 UINavigationController with their own root UIViewController for my UITabBar.
I created one custom UIViewController as template, in where my other UIViewControllers are sub-class.
On my template:
I have my rightBarButtonItem to show current user profile.
// public method added on template
- (void) goToProfile {
NSLog(#"going through...");
ProfileViewController *ctrl = [[ProfileViewController alloc] init];
[self.navigationController pushViewController:ctrl animated:YES];
}
For my leftBarButtonItem:
- (void) goBack {
[self.navigationController popViewControllerAnimated:YES];
}
First click on the rightBarButtonItem, works fine. If I click the leftBarButtonItem to go back then re-click the rightBarButtonItem, it won't work anymore.
In addition, I have a button on one of my UIViewController that is calling the public method goToProfile. And that works fine.
I got help from my co-worker. The approach is something similar to #Vidhyanand900 answer. I hope this will help others in the future.
tabbarSelectedIndex = 1; // profile tab index
ProfileViewController *ctrl = [[ProfileViewController alloc] init];
UINavigationController *navController = [_appDelegate.mainTabBarController.viewControllers objectAtIndex:tabbarSelectedIndex];
[navController pushViewController:ctrl animated:YES];
self.mainTabBarController.selectedIndex = tabbarSelectedIndex;
If you are pushing or popping with UINavigation controllers of UITabBar
Else show UINavigation Controllers inside UITab Bar..i.e; Is Profile View controller is part of UITab bar or not..
Then you need to change the index of tab bar as below
self.tabBarController.selectedIndex=0;//if profile is first tab
ProfileViewController *ctrl = [[ProfileViewController alloc] init];
[self.navigationController pushViewController:ctrl animated:YES];
Hope it helps you...
Hi I want implement this animation effect(using storyboard), when user click a tab bar(say 2nd tab bar item), it displays the corresponding view like a modal view(display upwards to the screen)
My current method is delegate tab bar controller to tab bar and rewrite
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSLog(#"rawr");
NSLog(#"inside tab bar did select, index is %d", self.selectedIndex);
if (item == [self.tabBar.items objectAtIndex:1]) {
//item.title = #"modal view";
NSLog(#"going to promote as modal view");
CBTab2ViewController* modalViewController = [[CBTab2ViewController alloc] init];
[self presentViewController:modalViewController animated:YES completion:nil];
}
}
The problem is, the view displayed is a black screen, any idea how to implement it correctly?
my source code can be found a link below:
https://www.dropbox.com/s/6rd6ek59xf0yiq4/testtabbar.zip
Change the lines :
CBTab2ViewController* modalViewController = [[CBTab2ViewController alloc] init];
[self presentViewController:modalViewController animated:YES completion:nil];
To this one (incase your storyboard name is "MainStoryBoard"):
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle: nil];
CBTab2ViewController * modalViewController = (MyViewController*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"CBTab2ViewController"];
[self presentViewController:modalViewController animated:YES completion:nil];
I'm working on an iPad application, with 3 UIViewController's:
The first one contains a UITableView. On "didSelectRowAtIndexPath" it opens a modal with the second ViewController, like that :
NavRapportViewController *nav=[[NavRapportViewController alloc]initWithRootViewController:secondViewController];
nav.modalPresentationStyle = UIModalPresentationFormSheet;
nav.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[XAppDelegate.window.rootViewController presentViewController:nav animated:YES completion:nil];
In this second view there is a button to open the third view :
GalleryLayout *layout = [[GalleryLayout alloc] init];
GalleryViewController *gallery = [[GalleryViewController alloc] initWithCollectionViewLayout:layout];
[gallery setLegID:monLeg.legId];
gallery.delegate = self;
[self.navigationController pushViewController:gallery animated:YES];
Last, in the third view there is a back button. When I press the back button I supposed to return on the second view. But in practice, the second view appear during less than one second and the modal dismiss itself.
I've try to override the backButton action like that :
-(void)backPressed:(id)sender {
[self.navigationController popToViewController:self.delegate animated:YES];
}
But the result is the same.
Do you know why the modal close itself, and how should I do to keep it open ?
The second view controller is the root view controller of navigation controller. you can just pop to root view controller.
-(void)backPressed:(id)sender {
//[self.navigationController popToRootViewControllerAnimated:YES];
//OR
[self.navigationController popViewControllerAnimated:YES];
}
My app it's embedded in a tab bar controller, but I'm using a nib in which I'm doing some operation.
At the end of this operation I've to pop to a view controller in my tab bar controller. I tried to use this code (suggested by an another user on here):
[self.tabBarController setSelectedIndex:<#(NSUInteger)#>];
[self.navigationController popToRootViewControllerAnimated:NO];
but it doesn't do anything. I guessed i should use this instructions:
[self dismissViewControllerAnimated:NO completion:nil];
but I've to put something in the completion, I don't know how to do that, can you help me?
Update:
I will show you my storyboard so you can understand what I'm trying to explain:
also in "Product View Controller", when I press on a button (I design the button by code) it shows me this file XIB:
In this view there's a button (usually it's hidden), when I press on this button it will add the product I'm looking to a remote cart. After the adding this product to the remote cart, I will pop to the "Carriage View Controller". When I pop to the "Carriage View Controller" I should pass them some data of my cart.
So my app has this tab view:
Home View Controller: the main view controller, loaded when the app is started
Category View Controller: it's a table view controller in which I'm displaying the category of products in my store
Carriage View Controller: a controller in which I will display the data of my cart
Info View Controller: a controller in which I display information about the app
From the "Category View Controller" with a segue I pop a View Controller in which I display by a custom view the products. When I press on one of this products it calls a the xib file I posted before file in which there are the details of the product selected before.
I hope you understand better what I'm trying to do.
CODE:
Here's the code to display the xib I posted:
- (void)collectionView:(PSCollectionView *)collectionView didSelectCell:(PSCollectionViewCell *)cell atIndex:(NSInteger)index {
int productID = (int)index;
productID = productID + 1;
if (IS_IPHONE_5) {
ProductScannedViewController *productScannedVC = [[ProductScannedViewController alloc]initWithNibName:#"ProductScannedViewControllerRetina4" bundle:nil];
productScannedVC.idProductScanned = [NSString stringWithFormat:#"%d", productID];
[productScannedVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:productScannedVC animated:YES completion:nil];
} else {
ProductScannedViewController *productScannedVC = [[ProductScannedViewController alloc]initWithNibName:#"ProductScannedViewController" bundle:nil];
productScannedVC.idProductScanned = [NSString stringWithFormat:#"%d", productID];
[productScannedVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:productScannedVC animated:YES completion:nil];
}
}
[self dismissViewControllerAnimated:YES completion:^{
// completion code here
}];
You can only use the popToRootViewControllerAnimated method if the view controller is part of that navigation controller's navigation stack.
If you are presenting the view controller like this
TestViewController * vc = [[TestViewController alloc] init];
[self presentViewController:vc animated:YES completion:^{
}];
And want to do the tab navigation inside I suggest injecting the tab bar as a weak property
TestViewController * vc = [[TestViewController alloc] init];
[vc setTabController:self.tabBarController];
[self presentViewController:vc animated:YES completion:^{
}];
Inside the TestViewController where you want to do the navigation:
[self dismissViewControllerAnimated:YES completion:^{
[tabController setSelectedIndex:0];
}];
I tested this code.