IOS/objective-c/xcode: Instantiate DetailView Controller in Code - ios

I had some code for launching a modal view controller created in storyboard with a storyboard ID. For a new situation, I am trying to adapt it to present a ViewController that is actually the detail view of a table in the main navigational system. I'm trying to jump around in the app if that is possible.
In this case, the VC should not be modal. Instead, I want to take the user to the normal detail view of a table.
To make it more challenging, I need the detail view to have the object data. Fortunately, this should be present in the starting VC.
Here is my code for launching a modal VC.
UIStoryboard *storyBoard = self.storyboard;
detailVC *newVC =
[storyBoard instantiateViewControllerWithIdentifier:#"detailView"];
//pass object to new VC
detailVC.object = _object;//pass data object
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: dareVC];
[self presentModalViewController:nav animated:YES];
Edit: Following code does launch non-modal VC. However, initial detail view is devoid of data. The data is saved so if I come back to it, the data is there but initially, I get generic screen with no data.
detailVC *secondViewController =
[self.storyboard instantiateViewControllerWithIdentifier:#"detail"];
[self.navigationController pushViewController:secondViewController animated:YES];
secondViewController.object=_object;

detailVC *secondViewController =
[self.storyboard instantiateViewControllerWithIdentifier:#"detail"];
secondViewController.object=_object;
[self.navigationController pushViewController:secondViewController animated:YES];
First instantiate it.
Then set the data.
Then push it.

Related

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

Initialize ViewController with data and programmatically show it to screen

The storyboard of my iOS application contains a NavigationController with a root ViewController. From a secondary view controller, I would like to programmatically display the root VC with the attached NavigationController showing at the top, and at the same time instantiate the root VC with some data.
I have the following so far in the secondary view controller:
UINavigationController* nav = [[UIStoryboard storyboardWithName:#"Storyboard" bundle:nil] instantiateViewControllerWithIdentifier:#"NavController"];
UIViewController* viewController = [[UIStoryboard storyboardWithName:#"Storyboard" bundle:nil] instantiateViewControllerWithIdentifier:#"RootVC"];
viewController.data = #"My test data";
[self presentViewController:nav animated:YES completion:nil];
The root VC displays successfully with the navigation bar along the top, however when I print the following in the rootVC logic, it comes out as null:
NSLog(#"Initialized data: %#", self.data); // null
How can I fix this? It seems that the initialized data is not coming through from the secondary controller.
You are creating a new instance of UIViewcontroller which has identifier "RootVC" and assigning data to it.
Instead, you should assign the data to rootViewController of navigation controller something like this:
self.navigationController.viewControllers.firstObject.data = #"My test data";
It is a problem of different instances of same class.

Navigation from one view to another view in IOS (objective c)

I am new in IOS. I am confused in navigation.I have looked 3,4 methods to navigate from one view to another view controller.
First
DashboardViewController *dashboard = [self.storyboard instantiateViewControllerWithIdentifier:#"DashboardViewController"];
[self.navigationController pushViewController:dashboard animated:YES];
Second :- Using push segue on clicking button.
Third :- I'm not clear for it, That is array of view controller. In which we get the view controller and then navigate.
Fourth:-
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Main"
bundle:nil];
UserDashboardViewController *users =
[storyboard instantiateViewControllerWithIdentifier:#"UserDashboardViewController"];
[self presentViewController:users
animated:YES
completion:nil];
I am confused which is best way for navigation, and most important how to navigate using array of view controllers. Please help me, Thanks.
This is the best way in my point of view.

presentViewController not working as I'd like

I have my presentViewController code set up like this:
ProductViewController *vc = [[ProductViewController alloc] init];
[self presentViewController:vc animated:YES completion:nil];
It indeed presents the vc controller, but I'm unable to tap anything on it, it's like a frozen screen. Is it because there is still a view on top of it? Or I shouldn't be using alloc] init?
ProductViewController is an existing view controller (it's actually the root view controller), so basically I'd just like the presentViewController to just go back to the root view controller.
Edit:
I have a ShippingViewController where the user enters name/shipping details.
Then they tap on "Checkout" button that presents the PaymentViewController (which has a navigation bar, all other View Controllers don't).
On the PaymentViewController, the user enters their credit card details and tap the "Pay" button.
If the payment is successful, I wish for ProductViewController (the initial/root view controller) to be presented. Currently if I am just calling dismissViewController, it presents ShippingViewController.
This seemed to take care of my problem:
NSString *storyboardName = #"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"id"];
[self presentViewController:vc animated:YES completion:nil];

Passing data between the Tabs of a TabBarViewController

I have a UITabBarController based application and I want to pass data from one view to another. I am doing this in storyboard and I am just doing some testing, before bringing it into the main application.
I am just trying this with a NSString at the moment.
I am able to pass data to the VC in question when I use a modal transition using this code:
NSString *sendingString = #"This string has some content";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
NextViewController *nVC = (NextViewController *)[storyboard instantiateViewControllerWithIdentifier:#"goToNextVC"];
nVC.receivingString = sendingString;
[self presentViewController:nVC animated:YES completion:nil];
Now this pushes that VC up and passes it as I want it, but instead of pushing up the VC I want it to be pushed to another Tab Bar.
Now I can flick to the desired TabBar with this code:
self.tabBarController.selectedIndex = 1;
Where I get stuck is, how do I send data to this ViewController???
You could either subclass your TabBarController, and add a property to it, or create a singleton (e.g. DataManager), to which all your ViewControllers will have access. You can pass your data to it.

Resources