Initiating a childViewController with a functioning "back" button. iOS - ios

I want to launch a childViewController of my initialView when it is launched from sliding or pressing a push notification.
Right now, I can launch to the childView, but the functions do not work. One of the labels I've initiated in the .m file is also not showing.
How can I make sure every function (back button(that pops back to parent), functionButtons in the view, segues it leads to) works properly.
This is my code for loading the view:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle: nil];
NotificationsViewController *viewController= [mainStoryboard instantiateViewControllerWithIdentifier:#"NotificationsViewController"];
UINavigationController *nav = [[UINavigationController alloc]
initWithRootViewController:viewController];
[_window setRootViewController:nav];

Problem:
The root view controller provides the content view of the window. Assigning a view controller to this property installs the view controller’s view as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed that is why you cannot see the back button because they no longer exist.
Solution:
Pushes the view controller onto the receiver’s , the navigation bar will provide navigation back to the previous view controller.
NotificationsViewController *viewController = [[NotificationsViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];

Related

IOS/Objective-C/Storyboard: Prevent ViewController From Launching Modally

I want a viewcontroller to launch using a show transition, not modally from the bottom. Normally when I use the following code that's what happens. However, in this case, it is launching as a modal controller from the bottom up. Is there a switch I don't know about or could something be set in Storyboard that is causing this VC to launch modally from the bottom instead of showing?
UIStoryboard *storyBoard = self.storyboard;
IDImportEventsOnboard *importEvents =
[storyBoard instantiateViewControllerWithIdentifier:#"importEventsOnboard"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: importEvents];
[self presentViewController:nav animated:YES completion: nil];
The VC is embedded in a navigation controller.
Should I be using showViewController directly to the targetVC without going through the Nav? Or a pushViewController What is a proper, robust way to show a VC with a show transition?
Thanks in advance for any suggestions.
In the above code you are 'presenting' a new NavigationController from a ViewController. In order to do a push/show transition, that needs to be done on an instance of a NavigationController. If your current ViewController is already in a NavigationController, you can push the new ViewController onto the current NavigationController stack. For Example:
UIStoryboard *storyBoard = self.storyboard;
IDImportEventsOnboard *importEventsVC =
[storyBoard instantiateViewControllerWithIdentifier:#"importEventsOnboard"];
[self.navigationController pushViewController:importEventsVC animated:YES];

Cannot click back button when open view from remote notirication

My iOS application using Objective C. It received a remote notification, when a user click it, it will open a particular view.
This view has a back button in the navigation tab. In normal scenarios can back to root view. However, when I open this view from a notification, cannot back to any view nor leaving the application.
Here is my spruce code:
1. from AppDelegate.m to open a particular view:
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewAnnouncement =[storyboard instantiateViewControllerWithIdentifier:#"Announcement"];
self.window.rootViewController = viewAnnouncement;
[self.window makeKeyAndVisible];
Action function to back to previous view (in viewClass.m):
- (IBAction)onBtnBackClicked:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
I believe the problem is, when I open this view from a notification, no parent view to let it back to. Can anyone help me, when user open this view from a notification, whether it goes to a root view or back to the previous opening app
EDIT : The previous answer didn't take in count the fact that you were in AppDelegate. Maybe this answer is better.
Turn your [self dismissViewControllerAnimated:YES completion:nil]
into :
UINavigationController *navigationController = [[UINavigationController alloc] init];
UIViewController *yourPreviousVC = [[UIViewController alloc] init];
[navigationController pushViewController:yourPreviousVC animated:YES];
If I'm wrong or if I missunderstood the question, do not hesitate to correct my answer.
Try this
Announcement *annouce = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"Announcement"];
HomeVC *home = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"HomeVC"];
[((UINavigationController *)self.window.rootViewController) setViewControllers:#[home,annouce] animated:YES];
And If app already open and you want to go previous page
Announcement *annouce = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"Announcement"];
UIViewController *visibleView = ((UINavigationController *)window.rootViewController).visibleViewController;
[((UINavigationController *)self.window.rootViewController) setViewControllers:#[visibleView,annouce] animated:YES];
I found the solution. My app has multiple views which I can go from a navigation list. When I want to back to main view, I just dismiss the current view.
Cuurently, a view is opened when I click a notification and I can't simply dismiss this view, Coz it considered as root view.
What I did to solve it, I connect my view (Announcement) by segue to main view, and set an identifier (for example: "leaveAnnouncementPage". Then, in your class just call this function in back button's fnction to back to the main view:
[self performSegueWithIdentifier:#"leaveAnnouncementPage" sender:self];
Here is also some useful links:
First
Second
Thank you guys for your help, and all the best

Back button doesn't work after pushViewController

This is how I am pushing a UIViewController :
[tabController setSelectedIndex:0];
UINavigationController *navController = [tabController selectedViewController];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
NotificationViewController *notificationController = (NotificationViewController*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"NotificationViewController"];
[navController pushViewController:notificationController animated:YES];
navController.navigationBarHidden = NO;
When NotificationViewController is visible and I tap Back button, nothing happens. The previous UIViewController had a gesture in UINavigationBar but I have removed it in the UIViewController's viewWillDisappear. But still the NotificationViewController's Back button doesn't work.
You probably call the pushViewController before the view is visible.
Make sure you don't call your pushViewController in the viewDidLoad.
Your pushViewController should be used only after viewDidAppear is called if you want your navigation controller to work properly with the back button. Otherwise, the navigation controller will add the title to the previous view controller without the redirection link.

iOS: Showing the UIViewControllers when UITabBar and UINavigationBar buttons cliked

I am new to iOS as my project depends on both navigation and the UITabBar controller.I have done the following steps
UITabBar contains 4 buttons and navigation bar contains 2 buttons i.e is common to all the screens
1)first i have taken the UITabBar controller and added four buttons to it
2)For each button to the UITabBar i have added the navigation controller
When i click the tabbar buttons all the views are showing fine and coming to UITabBar bar buttons I am facing the problem as below
suppose I am in UITabBar screen "A" and i clicked the navigation bar button i got the navigation screen ex as "Navscreen"that means now "A" contains
"Navscreen" when i clicked tabbar button "B" and came back to UITabBar button "A" still its showing the "Navscreen"
To avoid such cases in the "Navscreen" view controller i have added the code as below in the view will appear
-(void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
{
NSUInteger indexOfTab = [tabBarController.viewControllers
indexOfObject:viewController];
NSLog(#"Tab index = %u (%u)", indexOfTab);
if (indexOfTab==0)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main"
bundle:nil];
FirstViewController *firstview =
(FirstViewController *) [storyboard
instantiateViewControllerWithIdentifier:#"home"];
[self.navigationController pushViewController:firstview
animated:YES];
}
else if (indexOfTab==1)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main"
bundle:nil];
secondviewcontroller *secondview =
(secondviewcontroller *) [storyboard
instantiateViewControllerWithIdentifier:#"Medremainder"];
[self.navigationController pushViewController:secondview animated:NO];
}
if (indexOfTab==2)
{
UIStoryboard *storyboard = [UIStoryboard
storyboardWithName:#"Main" bundle:nil];
mymedView *mymed_view =
(mymedView *) [storyboard
instantiateViewControllerWithIdentifier:#"mymed"];
[self.navigationController pushViewController:mymed_view
animated:NO];
}
if (indexOfTab==3)
{
UIStoryboard *storyboard = [UIStoryboard
storyboardWithName:#"Main" bundle:nil];
Event_view *event_view =
(Event_view *) [storyboard
instantiateViewControllerWithIdentifier:#"Event_view"];
[self.navigationController pushViewController:event_view
animated:NO];
}
}
But in this case when click "Navscreen" its working and when I click the UITabBar "B" iam able to get the UITabBar screen.If i click again UITabBar "A" i can see "A" and when i click the UITabBar button "B" app is crashing.AS i am new to ios please help me with the possible solution if i make any thing wrong please suggest with the correct solutions.
In your didSelectViewController:, you seem to be pushing new controllers onto self.navigationController multiple times? Each time you select a tab this code will push another controller loaded from storyboard on top of what is already there. This is probably what is messing up your navigation items.
Should you not just be allocating separate navigation controllers once to each tab so this is all automatic?
In storyboard/code allocate separate navigation controllers for each tab. In each navigation controller, create it with a root controller based on what you are loading from storyboard for each tab. You only need to create this once somewhere in your main controller. Then allocate the navigation controllers to separate tabs in storyboard or in code set the array of controllers to be an array of your created navigation controllers.
It should then all be automatic without you needing to implement didSelectViewController: at all since each tab will simply cause the appropriate navigation controller to be presented and its associated navigation bar.

BannerViewController interferes with hidesBottomBarWhenPushed

I'm implememting a design based on the TabbedBanner example in the iAdSuite. I have a UINavigationController in the first tab. In that UINavigationController I have a view controller that simply has a button that pushes to another view controller. The pushed view controller is set to Hide Bottom Bar On Push in Interface Builder.
Here is the code where I'm setting up the UITabBarController.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary objectForKey:#"UIMainStoryboardFile"] bundle:[NSBundle mainBundle]];
_tabBarController = [storyboard instantiateViewControllerWithIdentifier:#"TabBarController"];
_tabBarController.delegate = self;
FirstViewController *firstView = [storyboard instantiateViewControllerWithIdentifier:#"FirstViewController"];
UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:firstView];
_tabBarController.viewControllers = #[[[BannerViewController alloc] initWithContentViewController:firstNav], ];
self.window.rootViewController = _tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Everything works except the TabBar does not get hidden when I push to the next view controller. I have tried hiding the TabBar using the Interface Builder check box as well as using nextViewController.hidesBottomBarWhenPushed = YES and neither way works.
If I remove the BannerViewController implementation, the TabBar hides exactly as it should.
It seems to me that the BannerViewController is interfering with the UINavigationController being able to hide the TabBar.
Is it possible to use Hides Bottom Bar When Pushed to hide the TabBar in this type of setup?
Thanks
Note: I realize that the code above only has one tab. I removed the other tabs for clarity.
I think this is happening because the BannerViewController itself is just a container viewController and it never actually pushes another view controller. The view controllers are pushed within the container.

Resources