I'm working in iOS app but I'm trying to present a tableview when I click a button in one of my scenes like it shows in the image bellow:
I tried this way:
- (IBAction)presentScene:(id)sender
{
NSString *storyboardName = #"Main";
NSString *viewControllerID = #"myScene";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
MyViewController *controller = (MyViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
[self presentViewController:_categoViewController animated:YES completion:nil];
}
But doesn't show the navigation to go back to the main table view or I can't even set the title of the tableview. Any of you knows how can present tableview?
try this:
[[self navigationController] pushViewController:YOUR_DESTINATION_VIEW_CONTROLLER animated:YES];
You should present the tableViewController with UINavigationController like so:
MyViewController *controller = (MyViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
[self presentViewController:navigationController animated:YES completion:nil];
EDIT:
You should add a UINavigationController in your storyboard like this:
If you do not want to show the navigationBar in your HomeVC then hide it:
// HomeVC.m
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
Finally, push the MyViewController:
- (IBAction)presentScene:(id)sender {
NSString *viewControllerID = #"myScene";
UIStoryboard *storyboard = self.storyboard;
MyViewController *controller = (MyViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
[self.navigationController pushViewController: controller animated:YES]
}
After experimenting with everything I finally found what I was looking for:
self.tabBarController.selectedIndex = 1;
Related
I have login and signup viewcontrollers in Login storyboard not in the Main storyboard.
Once signup or login is successful, then I am changing Login storyboard to Main. The following code works but when I select any tab, it does not call tab delegate method in the AppDelegate
However, if user is already succesfully signup or logged in, then it calls the following tabbar delegate method.
LoginViewController.m
if(isLoginSuccess)
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
CustomTabBarViewController *tbc = [mainStoryboard instantiateViewControllerWithIdentifier:#"tabbar"];
tbc.selectedIndex = 2;
[self presentViewController:tbc animated:YES completion:nil];
}
AppDeletage.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CustomTabBarViewController *tabBarController = (CustomTabBarViewController *)self.window.rootViewController;
tabBarController.delegate = self;
return YES;
}
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (tabBarController.selectedIndex == 2) {
if(![self isRegistered])
{
UIStoryboard *loginStoryboard = [UIStoryboard storyboardWithName:#"LoginStoryboard" bundle:nil];
UIViewController *vc = [loginStoryboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[ROOTVIEW presentViewController:vc animated:YES completion:nil];
}
else
{
tabBarController.selectedIndex = 2;
}
}
}
Update :
AppDelegate is not getting called but I wonder why the following code does not open Loginstoryboard in the AppDelegate after user is logout.
#define ROOTVIEW [[[UIApplication sharedApplication] keyWindow] rootViewController]
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"LoginStoryboard" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[ROOTVIEW presentViewController:vc animated:YES completion:nil];
Please find below code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard *main = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UITabBarController *tabController = [main instantiateViewControllerWithIdentifier:#"TabbarController"];
tabController.delegate = self;
return YES;
}
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
NSLog(#"Selected Index - %lu",(unsigned long)tabBarController.selectedIndex);
}
On ViewController's Button Click Method,
- (IBAction)btnLoginTapped:(id)sender {
UIStoryboard *main = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UITabBarController *tabController = [main instantiateViewControllerWithIdentifier:#"TabbarController"];
tabController.selectedIndex = 1;
[self presentViewController:tabController animated:YES completion:nil];
}
then on Main.storyboard, Drag Object from the Object Library and put it below First Responder in Tab bar Controller Scene, Set the Object class to AppDelegate, then right click on Tab Bar Controller and set delegate to that Object Class as shown in below image.
Let me know it is working or not, I'm ready to help with the solution.
I would like to push to ParcelPickUpViewController after I clicked a button in LocationDetailsTableViewCell xib. But I've written the code below and it able to run. But the result won't happen at all. Need help on this issue. Thanks.
In LocationDetailsTableViewCell.h
#property (nonatomic, strong) UINavigationController *viewController;
In LocationDetailsTableViewCell.m
- (IBAction)selectBtn:(id)sender {
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ParcelPickUpViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier: # "pickUpVC"];
[self.viewController pushViewController:vc animated: YES];
[self.viewController setNavigationBarHidden:YES animated:YES];
}
I think you have button in UITableViewCell.Now you want to navigate to next view from the button click.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:nil options:nil];
cell = [nibObjects objectAtIndex:0];
}
cell.btnSelct.tag = indexPath.row;
[cell.btnSelct addTarget:self action:#selector(selectBtn:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (IBAction)selectBtn:(id)sender
{
//This is for XIB
ParcelPickUpViewController *vc = [[ParcelPickUpViewController alloc]initWithNibName:#"pickUpVC" bundle:nil];
//If you use storyboard
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ParcelPickUpViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier: #"parcelpickup"];
[self.viewController pushViewController:vc animated: YES];
}
Try This ...
- (IBAction)selectBtn:(id)sender {
ParcelPickUpViewController *vc = [[HomeScreenVC alloc]initWithNibName:#"pickUpVC" bundle:nil];
[self.viewController pushViewController:vc animated: YES];
[self.viewController setNavigationBarHidden:YES animated:YES];
}
option 2
you are created the Navigation controller not allocated the memory of your instance navigation controller, so do like
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ParcelPickUpViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier: # "pickUpVC"];
self.viewController = [[UINavigationController alloc] init];
[self.viewController pushViewController:vc animated: YES];
[self.viewController setNavigationBarHidden:YES animated:YES];
if you no need the setNavigationBarHidden then use like
option 3
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ParcelPickUpViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier: # "pickUpVC"];
[self presentViewController:vc animated:YES completion:nil];
(1) Make sure you have set the isInitialViewController(the first controller you want to display when app launch) in storyboard.
(2) If you did not add UINavigationController in storyboard, create it programeticaly and set it as rootViewController w.r.t your InitialViewController in app delegate didFinishLaunchingWithOptions.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIStoryboard *storyboard = storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:Nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"Your_First_View_Controller_Identifier"];
UINavigationController *rootNavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = rootNavigationController;
return YES;
}
(3) While navigating to your ParcelPickUpViewController just simply add below code:
- (IBAction)selectBtn:(id)sender {
ParcelPickUpViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier: # "pickUpVC"];
[self.navigationController pushViewController:vc animated: YES];
}
I am using below code to navigate to another view controller, i want to assign this self to a delegate of another class, how do i do it ?
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *controller = [storyboard instantiateViewControllerWithIdentifier:#"storyBoardID"];
self.navigationController presentViewController:controller animated:YES completion:nil];
Get your view Controller from Navigation Controller
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *controller = [storyboard instantiateViewControllerWithIdentifier:#"navigationControllerID"];
TargetVC *targetVC = (TargetVC *)controller.viewControllers.firstObject
//OR
TargetVC *targetVC = (TargetVC *)controller.topViewController;
targetVC.mydelegate = self;
[self.navigationController presentViewController:controller animated:YES completion:nil];
Get your view Controller from TabBar Controller
UITabBarController *homeTabBar=[[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"homeTabBarID"];
TargetVC *targetVC =nil;
UINavigationController *nav = [homeTabBar.viewControllers objectAtIndex:0];
//if you have multiple tab then you can give Index as per your ViewController
targetVC =(TargetVC*)[nav.viewControllers objectAtIndex:0];
targetVC.mydelegate = self;
[self.navigationController presentViewController:homeTabBar animated:YES completion:nil];
I have two view controller named ViewController and SecondViewController. From the first view controller i m trying to call my SecondViewController after 5 second. but it will giving me black screen instead of my expected view.
Here is my view controller code,
`
#import "ViewController.h"
#import "SecondViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:#selector(loadingNextView) withObject:nil afterDelay:5.0f];
}
-(void)loadingNextView{
SecondViewController *SVC = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
SVC.view.backgroundColor = [UIColor redColor];
[self presentViewController:SVC animated:YES completion:NULL];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end`
Please suggest. If i used button for navigation then its work but my needs is to call automatically on timer.
If your SecondViewController is in storyboard instead of alloc init you have use instantiateViewControllerWithIdentifier
[self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
Dont forget to give the identifier for the controller in the storyboard. So the method will be
-(void)loadingNextView{
[self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
SVC.view.backgroundColor = [UIColor redColor];
[self presentViewController:SVC animated:YES completion:NULL];
}
If you are using storyboard then you cannot initialise the controller with the nib Method "initWithNibName" basically searches for an Xib file, which you do not have as you are using a storyboard, better initialise it in the following way ..
-(void)callNextView{
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:#"yourStoryBoarddName" bundle:nil];
yourViewController *VC = [storyboard instantiateViewControllerWithIdentifier:identifier];
[self presentViewController:VC animated:YES completion:NULL];
}
If you are not using the storyboard, you can simply init the new controller like this
SecondViewController *SVC = [[SecondViewController alloc]init];
[SVC setBackgroundColor: [UIColor redColor]];
[self presentViewController: SVC animated: YES completion: nil];
You need to initialize the controller exact from the storyboard.
- (void)loadingNetView
{
UIStoryboard storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
NSString *identifier = #"SecondController"; // you need to set identifier in storyboard
SecondViewController *controller = [storyboard instantiateViewControllerWithIdentifier:identifier];
}
I want to pass data between storyboard with tabBar and external xib .
I tried to use prepareforsegue but the method didn't call or if i define a segue this didn't recognize this .
my code is :
UIStoryboard *storyborad = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *view = [storyborad instantiateViewControllerWithIdentifier:#"<identifier>"];
[self.navigationController setNavigationBarHidden:YES];
[self.navigationController pushViewController:view animated:YES];
// [self performSegueWithIdentifier:#"try" sender:self]; // tried to use performSegueWithIdentifier but this recognize identifier
and the prepareforsegue method code :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
BIDViewController *viewControllerView = segue.destinationViewController;
viewControllerView.isViewPushed=1;
NSLog(#"prepareForSegue");
Instead of this:
UIStoryboard *storyborad = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *view = [storyborad instantiateViewControllerWithIdentifier:#"<identifier>"];
[self.navigationController setNavigationBarHidden:YES];
[self.navigationController pushViewController:view animated:YES];
Try this:
UIStoryboard *storyborad = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
BIDViewController *view = [storyborad instantiateViewControllerWithIdentifier:#"myIdentifier"];
view.isViewPushed = 1;
[self.navigationController setNavigationBarHidden:YES];
[self.navigationController pushViewController:view animated:YES];