Touchupinside and simple viewController question - objective-c

I've got a very simple viewController question .
I've got a main view with 2 buttons, upon touchupinside of a button I would like to load a new view Controller ( a table view ) .
I've created a new viewController with its nib file .
Created an IBAction like this that I've linked the button to in IB :
- (IBAction)displayVin {
UIViewController *vinController = [[UIViewController alloc] initWithNibName:#"vinController" bundle:nil];
[self.navigationController pushViewController:vinController animated:YES];
[vinController release];
}
It's not doing anything therefore I must be forgetting something, but what ? should I declare this newViewController somewhere else ? It's compiling and executing fine with no error message .
UPDATE:
I'm now using this :
- (IBAction)displayVin:(id)sender {
NSLog(#"here");
UIViewController *vinController = [[UIViewController alloc] initWithNibName:#"vinController" bundle:nil];
[self.navigationController pushViewController:vinController animated:YES];
[vinController release];
}
But it's crashing with this error when I press the button :
[displayVin]: unrecognized selector sent to instance 0x8d03000
** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[displayVin]: unrecognized selector sent to instance 0x8d03000'*
Should I be declaring something somewhere else , or "should" that bit of code be enough ?

Start by using NSLog() to determine if you're actually inside the displayVin() routine. Button events send a parameter to the acion, so you should probably declare it as -(IBAction)displayVin:(id)sender instead of just -(IBAction)displayVin.

For your updated code, you might have to reconnect the IBOutlet in the IB file. It looks like it's still calling the [viewController displayVin] method (without arguments), instead of the [viewController displayVin:]

Related

Uncaught exception in xcode

I'm new to Xcode and therefor i'm to build small apps. i've recently got a problem, i just cant seem to find.
i've made a send button, that is going to send me from ViewController2 to ViewController.
Viewcontroller2.h
#interface ViewController2 : UIViewController {
ViewController *viewController;
}
- (IBAction)SaveData:(id)sender;
ViewController.m
- (IBAction)SaveData:(id)sender {
viewController = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[self.view addSubview: viewController.view];
}
i've checked every connection in storyboard is correct, but i still get this error.
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
I'm not 100% sure on what exactly is causing the error. As discussed in comments, segues are a much easier way of dealing with transitions between view controllers, so unless you have a particularly good reason to not use segues, I'd start learning segues (since you're learning new stuff anyway).
The error, however, is probably related to this:
[self.view addSubview: viewController.view];
When you do this, what you seem to be trying to do is taking the view (visual part) and adding it to the view of ViewController2. Meanwhile, ViewController2 doesn't have any controls set up for that view, and you wrote a lot of code in viewController that is pointless because you're never actually instantiating a view with that controller.
The above line I posted should be replaced with the following if you're not going to use segues:
[self presentViewController:viewController animated:YES completion:nil];
And this should be called after setting the properties on viewController for the data you wish to pass.
Better, however, is to just use storyboard segues. No matter how you call your segue, you're going to pass data by overriding the prepareForSegue:sender: method:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:#"segueNameHere"]) {
ViewController* viewController = [segue destinationViewController];
// now set the properties of viewController
}
}

ECSlidingViewController not First View

My application start with a view thats not use ECSlidingViewController, and then have a button to another that uses it.
In switching the views using Storyboard Segue, but I'm getting error.
What should I add to btnGoSecondView to load ECSlidingViewController properly?
Code:
-(IBAction)btnGoSecondView:(id)sender {
[self performSegueWithIdentifier:#"SecondViewShow" sender:self];
}
Error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
i think you should make some initialview first, and make that view became initial view controller and make an identifier for every each view controller.
something like this ..
self.viewcontroller = [storyboard instantiateViewControllerWithIdentifier:#"home"];
Based on the error you're getting an object is trying to be inserted into a mutable array but it hasn't been initialised. Where is the exception thrown in your code?
Try work out where the array is being accessed and why it's null. The NSMutableArray may not have been initialised in which case you'll need to initialise it.
NSMutableArray *arrayName = [NSMutableArray new];
Another thought: Before the ECSlidingViewController is presented I think you need to set it's topViewController. You could add it in -prepareForSegue:sender: or in the viewDidLoad of the viewController to be presented.
Something like this perhaps:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
ECSlidingViewController *slidingViewController = (ECSlidingViewController *)segue.destinationViewController;
slidingViewController.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"FirstTop"];
}

Changing views works in viewDidLoad but not on a button click IBAction

I want to open up a new view with the identifier AddSourceViewController when a button is clicked. I'm using the following code to do so:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"AddSourceViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:YES];
When used in viewDidLoad on an initial view, this code works perfectly and opens up the additional view. However, I have added an IBAction to a button on the initial view and when I add the above code to attempt to open the new view on button click, I just get a Thread 1: breakpoint 5.1 6.1. How can I get this to work on the button action?
EDIT: Better error information below.
2012-11-23 16:33:40.399 Marketr[23454:c07] -[SecondViewController addSourceButton:]: unrecognized selector sent to instance 0x6e2f5c0
2012-11-23 16:33:40.400 Marketr[23454:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SecondViewController addSourceButton:]: unrecognized selector sent to instance 0x6e2f5c0'
Do you have a breakpoint set for your action method? Look on the left side next to the code of this method, to see if there is a blue arrow pointing towards the code.

"Application tried to present modally an active controller"?

I just came across a crash showing a NSInvalidArgumentException with this message on an app which wasn't doing this before.
Application tried to present modally an active controller
UITabBarController: 0x83d7f00.
I have a UITabBarController which I create in the AppDelegate and give it the array of UIViewControllers.
One of them I want to present modally when tapped on it. I did that by implementing the delegate method
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
If that view controller is of the class of the one I want to present modally, I return NO and do
[tabBarController presentModalViewController:viewController animated:YES];
And now I'm getting that error, which seems to mean that you can't present modally a view controller that is active somewhere else (in the tabbar...)
I should say I'm on XCode 4.2 Developer Preview 7, so this is iOS 5 (I know about the NDA, but I think I'm not giving any forbidden details). I currently don't have an XCode installation to test if this crashes compiling against the iOS4 SDK, but I'm almost entirely sure it doesn't.
I only wanted to ask if anyone has experienced this issue or has any suggestion
Assume you have three view controllers instantiated like so:
UIViewController* vc1 = [[UIViewController alloc] init];
UIViewController* vc2 = [[UIViewController alloc] init];
UIViewController* vc3 = [[UIViewController alloc] init];
You have added them to a tab bar like this:
UITabBarController* tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:vc1, vc2, vc3, nil]];
Now you are trying to do something like this:
[tabBarController presentModalViewController:vc3];
This will give you an error because that Tab Bar Controller has a death grip on the view controller that you gave it. You can either not add it to the array of view controllers on the tab bar, or you can not present it modally.
Apple expects you to treat their UI elements in a certain way. This is probably buried in the Human Interface Guidelines somewhere as a "don't do this because we aren't expecting you to ever want to do this".
I have the same problem. I try to present view controller just after dismissing.
[self dismissModalViewControllerAnimated:YES];
When I try to do it without animation it works perfectly so the problem is that controller is still alive. I think that the best solution is to use dismissViewControllerAnimated:completion: for iOS5
In my case i was trying to present the viewController (i have the reference of the viewController in the TabBarViewController) from different view controllers and it was crashing with the above message.
In that case to avoid presenting you can use
viewController.isBeingPresented
!viewController.isBeingPresented {
// Present your ViewController only if its not present to the user currently.
}
Might help someone.
The same problem error happened to me when I tried to present a child view controller instead of its UINavigationViewController parent
I had same problem.I solve it. You can try This code:
[tabBarController setSelectedIndex:1];
[self dismissModalViewControllerAnimated:YES];
For React Native Developer - Problem might not be in AppDelegate Or main.m if app has been successfully build and is running and will crash after splash or perhaps the error screen
Issue might be due to use of fonts/resources that is not available with xcode and not properly configured.. You can find out the error by commenting certain portion starting from App.js and drilling inside the navigation/screens and commenting the components till you find the component that is generating the error....
In my case the resource of fontFamily was making an issue which was used right after splash in walkthrough screen
<Text style={{fontFamily: Fonts.roboto}}>ABC</Text>
Here font roboto wasnot configured properly. Wasted entire days just debugging the error hope its helps you
In my case, I was presenting the rootViewController of an UINavigationController when I was supposed to present the UINavigationController itself.
Just remove
[tabBarController presentModalViewController:viewController animated:YES];
and keep
[self dismissModalViewControllerAnimated:YES];
Instead of using:
self.present(viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?)
you can use:
self.navigationController?.pushViewController(viewController: UIViewController, animated: Bool)
This is my way which supporting multiple Windows(from a single APP) on the iPad and nested modal present.
import UIKit
///✅Use this public method
public func SheetViewController(ViewController:UIViewController) {
for i in returnAvailableViewControllers().shuffled() {
if i.presentedViewController == nil && !ViewController.isViewLoaded {i.present(ViewController, animated: true, completion: {})}
}
}
///Returns all possible ViewControllers
private func returnAvailableViewControllers() -> [UIViewController] {
let 场景 = UIApplication.shared.connectedScenes
var 存储VC : [UIViewController] = []
for i in 场景 {
if i.activationState == .foregroundActive {
//⭐️Set up “foregroundActive” to give the user more control
var 视图控制器 = (i.delegate as? UIWindowSceneDelegate)?.window??.rootViewController
if 视图控制器 != nil {
存储VC.append(视图控制器!)
}
var 结束没 = true
while 结束没 {
//🌟Enumerate all child ViewController
视图控制器 = 视图控制器?.presentedViewController
if 视图控制器 != nil {
存储VC.append(视图控制器!)
} else {
结束没.toggle()
}
}
}
}
return 存储VC
}

UISplitViewController reload data

How can I call function from UISplitViewController subclass to other ViewController.
I have an application that displays splitview and shows modal popup after. When popup is closed, I need to reload data. So appdelegate looks like this:
[self.window addSubview:splitview.view];
[splitview presentModalViewController:popup_ctrl animated:YES];
On TableViewController I've made funcion reloadData;
I found out that by subclassing UISplitViewController, I can detect closing of popup with -(void)viewDidAppear:(BOOL)animated. I've tried following code:
[[self.viewControllers objectAtIndex:0] reloadData];
However it crashes with NSInvalidArgumentException: unrecognized selector.
What do I have to modify to make the function reloadData work?
Why not explicitly add a property to your SplitViewController that references your TableViewController?
Then you can say
mySplitViewController.TableViewController = myTableViewController;
and later call
[mySplitViewController.TableViewController reloadData];