I've tried to put following two lines into - (void)viewDidLoad.
self.tabBarController.selectedIndex = 3;
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:3];
At that time, only Tabbar index to change 3 but View Controller remained at default view controller, mean 0. Is there any thing missing in my coding?
Solution: Try by moving your,
self.tabBarController.selectedIndex = 3;
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:3];
portion of your code to UIVIewController's
- (void)viewDidAppear:(BOOL)animated
I hope it will help you
Reason: You have to let your UIViewController to load fully first before you trigger an extra action on it.
Try this definitely it works:
UINavigationController *nc = [[self.tabBarController viewControllers] objectAtIndex:0];
id vc = [[nc viewControllers] objectAtIndex:0];
if ([vc isKindOfClass:[YourViewController class]])
{
[[self appDelegate].tabBarController setSelectedIndex:3];
}
Related
I Am Working on an Application where there is a TabBar present on My Menu Screen.
It has 5 view Controllers (5 Tabs). I am assigning 2 view controllers at the index 3 and 4 respectively, so that if i click on any of the two, they will navigate to their desired view Controllers.
Every thing works well if i select an item in tab bar for the first time.
But, when i come back to that view controller and try to press it again , then the item gets selected for first click, but does not navigate to the view controller.I have to click it again to perform that same action. i.e. I have to click it twice for Navigation.
Here i have posted some code regarding my question.
//...In my appdelegate class
CalenderScreen *calender=[[CalenderScreen alloc]initWithNibName:#"CalenderScreen" bundle:nil];
NewsScreen *news=[[NewsScreen alloc]initWithNibName:#"NewsScreen" bundle:nil ];
UINavigationController *nav1=[[UINavigationController alloc]initWithRootViewController:menu];
UINavigationController *nav2=[[UINavigationController alloc]initWithRootViewController:emergency];
UINavigationController *nav4=[[UINavigationController alloc] initWithRootViewController:help];
UINavigationController *nav3=[[UINavigationController alloc]initWithRootViewController:calender];
UINavigationController *nav5 =[[UINavigationController alloc]initWithRootViewController:news];
//tab bar initialization
tab=[[UITabBarController alloc]init];
tab.viewControllers=[[NSArray alloc]initWithObjects:nav1,nav2,nav4,nav5,nav3 ,nil];
UITabBarItem *tabItem = [[[tab tabBar] items] objectAtIndex:0];
[tabItem setTitle:#"Menu"];
tabItem.image=[UIImage imageNamed:#"Menu.png"];
UITabBarItem *tabItem1 = [[[tab tabBar] items] objectAtIndex:1];
[tabItem1 setTitle:#"Emergency"];
tabItem1.image=[UIImage imageNamed:#"Emergency.png"];
UITabBarItem *tabItem2 = [[[tab tabBar] items] objectAtIndex:2];
[tabItem2 setTitle:#"HelpLine"];
tabItem2.image=[UIImage imageNamed:#"helpline.png"];
UITabBarItem *tabItem3 = [[[tab tabBar] items] objectAtIndex:3];
[tabItem3 setTitle:#"News"];
tabItem3.image=[UIImage imageNamed:#"News.png"];
UITabBarItem *tabItem4 = [[[tab tabBar] items] objectAtIndex:4];
[tabItem4 setTitle:#"Raise Ticket"];
tabItem4.image=[UIImage imageNamed:#"ticket.png"];
self.window.rootViewController=tab;
[self.window makeKeyAndVisible];
And in my view controller class, I am having below code
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NewsScreen *news=[[NewsScreen alloc] initWithNibName:#"NewsScreen" bundle:nil];
if (tabBarController.selectedIndex==3)
{
NSUInteger index=3; //assign value here
UINavigationController *nv = [[tabBarController viewControllers] objectAtIndex:index];//index of your NewsScreen controller
[tabBarController setSelectedIndex:3];
NSArray *array =[nv viewControllers];
for (news in array)
{
if ([news isKindOfClass:[NewsScreen class]])
{
news.loginbacklbl.hidden=YES;
}
}
}
My worry is, when i click on NewsScreen for the first time, it works fine. But when i come back and try to click the NewScreen item on tabBar, it requires two clicks for me two get the event done. Can anybody help me why this happens, and what should be done to avoid this issue. Please help me out.
Any help is appreciated. Thank you.
Single click is used to activate your current tab & double click to go t root... Tell client this is default behavior.
If you still want to override, you have to make custom tab bar.
I want to make a segue that makes navigation controller poptoroot and then make a new controller pushed into the navigation controller .
e.g Now the controller hierarchy is
navigationController->rootcontroller->controllerA ->controllerB
and i want to make a segue perfome in controller B,and the segue will make the hierarchy to
navigationController->rootcontroller-> controllerC
is this possible?
You want to set segue doing pop to root. This can be achieved using Unwind Segues. For more detail regarding this you can follow this Apple doc.
https://developer.apple.com/library/content/technotes/tn2298/_index.html#//apple_ref/doc/uid/DTS40013591-CH1-TNTAG2-ADDING_AN_UNWIND_SEGUE_TO_A_STORYBOARD
For pushing to new VC, You can simply push in storyboard.
You can use for loop to pop till root or where you want and use same method to push forward:
// For Pop
NSArray *viewControllers = [[self navigationController] viewControllers];
for( int i=0;i<[viewControllers count];i++){
id obj=[viewControllers objectAtIndex:i];
if([obj isKindOfClass:[ViewControllerYouWantToStopOn class]]){
[[self navigationController] popToViewController:obj animated:YES];
return;
}
}
//For Push
NSArray *viewControllers = [[self navigationController] viewControllers];
for( int i=0;i<[viewControllers count];i++){
id obj=[viewControllers objectAtIndex:i];
if([obj isKindOfClass:[ViewControllerYouWantToJumpOn class]]){
[[self navigationController] pushViewController:obj animated:YES];
return;
}
}
This code work for me :
self.performSegue(withIdentifier: "segue_id", sender: self)
var navigationArray = self.navigationController?.viewControllers
var i = navigationArray!.count - 2
while i > 0 {
navigationArray!.remove(at: i)
i = i - 1
}
self.navigationController?.viewControllers = navigationArray!
After pushing a ViewController using this code
UIViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier:#"frontCardViewController"];
[self.navigationController pushViewController:vc animated:YES];
I remove all ViewControllers I don't need anymore using this code
NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
NSArray *array = [[NSArray alloc] initWithArray:navigationArray];
for (UIViewController *viewController in array) {
if ([viewController isKindOfClass:[RITCardViewController class]]) {
RSLog(#"Is kind of Class RITCardViewController");
[navigationArray removeObject:viewController];
}
}
self.navigationController.viewControllers = navigationArray;
The navigation array now looks like this:
"RITMainViewController: 0x10d81fc1>",
"RITDetailViewController: 0x10d847880",
"RITTestResultViewController: 0x113d0e090"
But the problem is that if the back button in the navigation bar is pressed now, it goes back to the second screen. But when the back button is pressed again it just stays on this screen. It seems to go trough all the screens I have removed, but doesn't show them.
What am I doing wrong?
Try something like this instead:
NSMutableArray *viewControllers = [#[] mutableCopy];
for (UIViewController *vc in self.nagivationController.viewControllers)
{
if (NO == [vc isKindOfClass:[RITCardViewController class]])
[viewControllers addObject:vc];
}
self.navigationController.viewControllers = viewControllers;
Make sure you don't corrupt the stack by removing the view controller you are currently on from the view controllers array. Assuming your current view controller is not an instance of RITCardViewController you should be fine. Otherwise, you'll have to make up for it in your code.
I need to pass an NSString from one view to another. I do in my first view controller:
-(IBAction)btnCreate_click:(id)sender
{
CreateMatchTableViewController *matcObj = [[CreateMatchTableViewController alloc]init];
matcObj.createBtnPressed = #"pressed";
[self.tabBarController setSelectedIndex:1];
}
CreateMatchTableViewController is my second view controller and createBtnPressed ia NSString object in second view controller.
In viewDidLoad of second view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
// MatchListViewController *obj = [[MatchListViewController alloc]init];
NSString *stringFromFirstView = createBtnPressed;
NSLog(#"check===>%#",stringFromFirstView);//...always null
}
Please help!!
Seems that in your first view controller you are creating a new instance of CreateMatchTableViewController. You do not want that because you have your CreateMatchTableViewController instance already in the tab bar controller...
Try something like this:
-(IBAction)btnCreate_click:(id)sender
{
CreateMatchTableViewController *matcObj = [self.tabBarController.viewControllers objectAtIndex:1];
matcObj.createBtnPressed = #"pressed";
[self.tabBarController setSelectedIndex:1];
}
EDIT
-(IBAction)btnCreate_click:(id)sender
{
UINavigationController *navigationController = [self.tabBarController.viewControllers objectAtIndex:1];
CreateMatchTableViewController *matcObj = [[navigationController viewControllers] objectAtIndex:0];// I put 0 assuming you have a CreateMatchTableViewController as the navigation controller's root view controller
matcObj.createBtnPressed = #"pressed";
[self.tabBarController setSelectedIndex:1];
}
on CreateMatchTableViewController write a method like following:
(void)setStringFromFirstViewController:(NSString*)str{
NSString *stringFromFirstView = str;
NSLog(#"check===>%#",stringFromFirstView);
}
Now on "first view controller" do the following:
-(IBAction)btnCreate_click:(id)sender{
CreateMatchTableViewController *matcObj = [[CreateMatchTableViewController alloc]init];
[matcObj setStringFromFirstViewController: #"pressed"];
[self.tabBarController setSelectedIndex:1];
}
Try
NSString* a = [[NSString alloc] initWithString:createBtnPressed];
I want to select the UIViewController at index 0 of my tabbarcontroller while passing data to it. It seems I am accessing the UINavigationController instead. Anyone know why this occurs?
SearchViewController *search = (SearchViewController *)[self.tabBarController.viewControllers objectAtIndex:0];
[search initWithText:#"This is a test"];
[[self.tabBarController.viewControllers objectAtIndex:1] pushViewController:search animated:NO];
// Also receive the error using this:
self.tabBarController.selectedViewController = search;
Error:
-[UINavigationController initWithText:]: unrecognized selector sent to instance
Wow, now I feel silly. This worked.
UINavigationController *navController = [self.tabBarController.viewControllers objectAtIndex:0];
SearchViewController *search = [navController.viewControllers objectAtIndex:0];
[search initWithText:#"This is a test"];
self.tabBarController.selectedViewController = navController;