push view controller by search button - ios

i have Storyboard with UINavigationController i have created the first screen with searchbar
i have added second viewcontrol via IB to storyboard
how do i push the second viewcontroller to navigationcontroller ?
i have tried
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
ViewController *screen2 = [[ViewController alloc] initWithNibName:#"ViewController2" bundle:nil];
[self.navigationController pushViewController:screen2 animated:YES];
}
i have set Custom Class of second view controller to ViewController2
but when i run the app and click the search button in keyboard i get the following error
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'ViewController2''

The error you are getting is because Xcode cannot find the xib named ViewController2.
You may just call self.performSegueWithIdentifier(#"identifierToViewController")
But first make sure that your searchViewController is a child of an UINavigationController
Then make sure that the segue has a proper identifier:

First of all make sure that your second view controller class name is : ViewController2
After that in storyboard give your View Controller's class name and Storyboard Id as #"ViewController2", Please refer screen shot.
Actually you use wrong method to create instance of UIViewController as it is used when you use xib file.
Change your code like this :
ViewController2 *screen2 = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController2"];
[self.navigationController pushViewController:screen2 animated:YES];

Related

Instantiating a view controller from another storyboard getting a key value coding-compliant error

I am attempting to add a UITableViewController subclass from another storyboard into the current on as one of the tab bar view controllers.
I am using the following code which is working for other view controllers.
UIViewController *vc;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
if (storyboard)
{
vc = [storyboard instantiateViewControllerWithIdentifier:#"ABCCustomViewControllerStoaryboardID"];
}
This gives me the following error:
* Terminating app due to uncaught exception
'NSUnknownKeyException', reason: '[
setValue:forUndefinedKey:]: this class is not key value
coding-compliant for the key fitnessStatusView.'
The fitnessStatusView is an outlet to a subview of one of the Static cells.
If I remove the outlet connection to this subview the problem goes away.
Is it not possible to connect a view in this way? It works fine when used in the storyboard that it is created in.
Any suggestions as to what I'm doing wrong here? Thanks.
Try this code :
UIStoryboard *loStoryboard ;
loStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
This code for navigate from AppDelegate Class..
// Instantiate the initial view controller object from the storyboard
UIViewController *initialViewController = [loStoryboard instantiateInitialViewController];
// Instantiate a UIWindow object and initialize it with the screen size of the iOS device
self.view = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Set the initial view controller to be the root view controller of the window object
[self.navigationController pushViewController:initialViewController animated:YES];
This code for navigate from Tabbar Controller..
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"ABCCustomViewControllerStoaryboardID"];
[self.navigationController pushViewController:vc animated:YES];
Nope, you're not supposed to attach outlets from View Controllers to UITableViewCell subviews. Yes, I understand that the storboard allows it but it won't work in runtime as you (and I) have already found out.
I don't know why you need an outlet to a static cell subview though, because by definition, you shouldn't have to modify it because it's static.
If you really want an outlet to it, you can create a UITableViewCell subclass, assign it to the cell with the subview you want and then you can create the outlet from the subview in the storyboard to the UITableViewCell subclass. You can access the outlet in tableView:cellForRowAtIndexPath. Although that would make your table view dynamic. :)
I figured out the problem. It was related to the fact I testing another target and the table view subclass was not included in that target.
I must admit it would have been nice if I got a class not found error.

Instantiate ViewController with identifier causes a crash using storyboard and a xib

So i'm using a storyboard AND a xib.
I use a tableview in my ViewController and it has a custom header as a xib file (the VC is the File Owner).
I want to present my new VC when swiping on the header, I added a swipe gesture to the xib using IB, but now i'm having problem presenting it.
Crash comes when i'm trying to instantiateViewControllerWithIdentifier. I made a property of my presented VC. And set the right Identifier "swipeRight".
- (IBAction)swipedRight:(id)sender {
NSLog(#"right");
if (_mpvc == nil) { //user those if only so the use wont push it twice and more
_mpvc = [self.storyboard instantiateViewControllerWithIdentifier:#"swipeRight"];
}
[self presentViewController:_mpvc animated:YES completion:nil];
}
The error:
Cannot find executable for CFBundle 0x9022120 </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk/System/Library/AccessibilityBundles/GeoServices.axbundle> (not loaded)
If you are inside a XIB file, how do you know which storyboard are you using? So first you have to instantiate storyboard which contains that viewController with specified identifier:
UIStoryboard *st = [UIStoryboard storyboardWithName:#"StoryboardName" bundle:nil];
After you have to instantiate viewController with this storyboard that you created:
UIViewController _mpvc = [st instantiateViewControllerWithIdentifier:#"swipeRight"];
Then finally you have to present the viewController:
[self presentViewController:_mpvc animated:YES completion:nil];
Hope it solves your problem!

ios change view on button click

I am new in ios development i know pretty simple , i want to change view on button click
this is my code i create Tabbed Application ,storyboard flow is
TabView Controller-> Navigation Controller
->HomeViewController->Navigation Controller->ShopViewController
Code snippet:
#implementation HomeViewController
#synthesize users;
- (IBAction)shopButton:(id)sender {
NSLog(#"hi sachin");
NSLog(#"INSIDE Shops");
ShopViewController *cvc = [[ShopViewController alloc]initWithNibName:#"ShopViewController" bundle:nil];
[self.navigationController pushViewController:cvc animated:YES];
}
Its showing an error like this:
Pacific1[2556:70b] hi sachin
2014-02-25 16:34:49.374 Pacific1[2556:70b] INSIDE Shops
2014-02-25 16:34:49.632 Pacific1[2556:70b] *** Terminating app due to uncaught
exception 'NSInternalInconsistencyException', reason: 'Could not load
NIB in bundle: 'NSBundle (loaded)' with name 'ShopViewController''
If you are using a storyboard, you should instantiate your view controller using the instantiateViewControllerWithIdentifier method.
Example:
ShopViewController *viewController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"ShopViewController"];
Make sure that the identifier is correct. It's under the Identity Inspector tab in Interface Builder. It's called Storyboard ID. You can give it any unique name that you want. Also make sure that story board name matches the name of your storyboard (without file extension).
Then:
[self.navigationController pushViewController:viewController animated:YES];
Your "ShopViewController" xib does not exists. See:
Terminating app due to uncaught
exception 'NSInternalInconsistencyException', reason: 'Could not load
NIB in bundle: 'NSBundle (loaded)' with name 'ShopViewController''
"Could not load
NIB in bundle"
If you're using storyboards you may want to instantiate the view like this:
ShopViewController *cvc = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"ShopViewController"];
Not 100% on that, but someone else may be able to point you in the right direction as I don't use storyboards.
ShopViewController *cvc = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"youstoryboardID"];
If still it,s not navigating than in story board select MainViewcontroller and click on Editor>Embed In in that select NavigationController.
I think it will work.

IOS opening other view controller on buttonclick not working with storyboards

I am having a silly problem with opening another viewcontroller on a buttonclick.
On my viewcontroller on my storyboard, I have a button. When a users clicks on this button, a new viewcontroller should be opened. As far as I know, there are 2 ways to do this.
in code
on the storyboard itself
Both methods are not working for me.
First the problem with storyboards:
(source: livefilestore.com)
I have connected my mainViewController with a navigationController, and I have connected my button with my next view (for adding an item). However, when I run the program, I get the following error:
'NSGenericException', reason: 'Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
I really don't understand this error, because the mainviewcontroller IS managed by a UINavigationController.
So, as this didn't work, I tried to handle it in code. I connected the buttonclick action to the mainViewController.h file and implemented it in the .m file.
This is the action method when a user clicks on the button:
- (IBAction)actionBtnAdd:(id)sender {
AddInventoryViewController *addinvController = [self.storyboard instantiateViewControllerWithIdentifier:#"AddInventoryViewController"];
[self presentViewController:addinvController animated:NO completion:nil];
}
The other viewcontroller is in the same storyboard, and the ID of the other viewcontroller is "AddInventoryViewController". This gives me the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
Anybody who know the problem? What would be the best approach to solve this?
Your navigation controller is not the initial view controller. Select your nav controller on storyboard, go into Attributes Inspector and select "Is Initial View Controller".
Make your navigation controller Initial View Controller.
Then create a push-segue from button to target controller.
Then you can use following method to make some adjustments before pushing:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"TargetSegue"]) {
TargetViewController *vc = segue.destinationViewController;
//vc.items = nil;
}
}
Update:
There are more options:
If your initial view controller is tabbar: Add segue from one of the tabs to navigation controller which contains your view controller.
If your initial view controller is another navigation controller:
Remove navigation controller which contains your view controller. Then add the segue between the thirdViewController and the source from which it is presented. The you can use push segue between the button and the target view controller.
If you want to do it programmatically:
Remove navigation controller which contains your view controller. Push the thirdViewController from your currently presented navigation controller. Push the targetViewController from buttonAction.
Hope it will help :)
In your Xcode .Click the storyboard on the left side and on top select Editor->EmbedIn->Navigation controller .It will automatically add your navigation controller to the first view controller .Then it would work
You have to set the Navigation controller as the first controller
See before the Navigation controller there is the storyboard starting arrow
Under your IBAction method instead of using
[self presentViewController:addinvController animated:NO completion:nil];
use this code and check
[self.navigationController pushViewController:addinvController animated:YES];

Segue from segmentcontrol in XIB Viewcontroller to Storyboard Viewcontroller

I have a segment control in a viewcontroller (UISegmentController.h, UISegmentController.m, UISegmentController.xib) created in a xib. Now I am trying to segue (ideally push) to Storyboard viewcontroller (ChartviewController.h, ChartviewController.m) I created. This Viewcontroller will hold images. I tried to perform this push as below:
-(IBAction) segmentedControlIndexChanged;
{
//some code
ChartViewController *chartviewpage = [[ChartViewController alloc] initWithNibName:#"ChartViewController" bundle:nil];
[self.navigationController pushViewController:chartviewpage animated:YES];
//some code
}
I used to implement this code to another xib viewcontroller and it worked fine. But now not with the stroryboard viewcontroller.
However, I receive an exception as follows:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'ChartViewController''
* First throw call stack:
(0x15e6012 0x11b3e7e 0x15e5deb 0x53ffac 0x404e37 0x405418 0x405648 0x405882 0x405b2a 0x41cef5 0x41cfdb 0x41d286 0x41d381 0x41deab 0x41e4a3 0x41e098 0x4048 0x11c7705 0x327920 0x3278b8 0x3e8671 0x3e8bcf 0x3e86a6 0x43d597 0x43f83b 0x35716d 0x357552 0x3353aa 0x326cf8 0x22d1df9 0x22d1ad0 0x155bbf5 0x155b962 0x158cbb6 0x158bf44 0x158be1b 0x22d07e3 0x22d0668 0x32465c 0x1d0d 0x1c35)
libc++abi.dylib: terminate called throwing an exception
(lldb)
Can u help please?
Thanks very much!
I finally figured it out doing a bit more research. first, one needs to instantiate the controller (ChartViewcontroller) in the storyboard they are trying to transition to. Then you need to give a storyboard ID name (ChartID) to the controller which can be find under the file inspector after selecting the controller. Finally perform the push to the new controller.
Hope this helps someone.
UIStoryboard * myStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ChartViewController *chartpage = [myStoryboard instantiateViewControllerWithIdentifier:#"ChartID"];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:chartpage animated:YES];

Resources