I have a simple app with only location services and 3 (almost empty) different views, and from some reason I can't get from view 1 to view 2 - app crashes and I get an exception. View 1 is the original .xib file, the two others are just views that I added later. It's weird cause I can switch between all of them (1->3, 2->1, 2->3, etc..) just not from 1->2.
I use this code in #1 view controller m. file:
- (IBAction) switchToMaps : (id)sender //this is the one that doesnt work
{
MyMap *mapsView = [[MyMap alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:mapsView animated:YES];
}
- (IBAction) switchToThird : (id)sender
{
ThirdView *third = [[ThirdView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:third animated:YES];
}
and as another example, here is the code from the 2nd view controller (MyMaps.m):
- (IBAction) switchBack : (id)sender
{
LastLocationViewController *firstView = [[LastLocationViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:firstView animated:YES];
}
- (IBAction) switchFront : (id)sender
{
ThirdView *lastView = [[ThirdView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:lastView animated:YES];
}
I know it's super vague, but any ideas what can cause this? I have no idea how to debug this...I even put breakpoints at the beginning of each IBAction method, and when it crashes, it doesnt even stop there....before I added this code, this app (which has only location) worked totally fine.
Any ideas?? Thanks!!
if your view does not load from any nib file then you should do like
MyMap *mapsView = [[MyMap alloc] init];
and
ThirdView *lastView = [[ThirdView alloc] init];
and in your back method
- (IBAction) switchBack : (id)sender
{
// LastLocationViewController *firstView = [[LastLocationViewController alloc] initWithNibName:nil bundle:nil]; // because you are allocating new memory to your last view
// [self presentModalViewController:firstView animated:YES];
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction) switchFront : (id)sender
{
// ThirdView *lastView = [[ThirdView alloc] initWithNibName:nil bundle:nil];
// [self presentModalViewController:lastView animated:YES];
[self dismissModalViewControllerAnimated:YES];
}
My hunch is that you're throwing an exception because
MyMap *mapsView = [[MyMap alloc] initWithNibName:nil bundle:nil];
is failing to load a nib. Without seeing your console output it's impossible to say for sure. So a few things to try:
Comment out [self presentModalViewController:mapsView animated:YES];, see if it still crashes.
Explicitly name the nib you expect to load. The nib loader assumes the nib is named exactly the same as the view controller if you pass in nil. So if you don't have a match, you'll end up with an exception (Like this [[MyMap alloc] initWithNibName:#"NibNameWithoutExtension" bundle:nil];)
Set a breakpoint at [self present... and then hover your mouse over "mapsView" after execution pauses. If the popup thing shows you mapsView is nil, you know your trouble is trying to pass a nil object to -presentModalViewController:animated:. If your breakpoint never hits because you throw an exception first, well, there you go, the trouble is a line above.
edit:
One more thing. If your nib has a button that's wired to an action that no longer exists, that would definitely get you in trouble. Inspect each button and make sure no actions are labeled in yellow, indicating a mismatch between the button's target and the actions it's reporting to IB. This would definitely account for the breakpoint behavior you described.
Related
I'm trying to move an NSString between two View Controllers and after searching around all convoluted ways, the easiest and most straight-forward way I want to get used to was to write an initWithName function in the Receiving VC and calling it in the Sending VC. It does move it successfully but I want it to get executed before ViewDidLoad loads the textViewer so that it shows right after the tab button is pressed. Here's the code from the sending VC:
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.text isEqualToString: #""]) {
textView.text = #"*Paste the machine code in question here*";
}
SecondViewController *theVCMover = [[SecondViewController alloc] initWithName: textView.text];
[self.navigationController pushViewController:theVCMover animated:YES]; //Is this really necessary if I'm not going to segue it directly, I'm just waiting for the user to press the next tab
gotItLabel.text = #"Got it! Ready for action...";
}
And here's the code on the receiving VC:
- (id)initWithName:(NSString *)theVCMovee {
self = [super initWithNibName:#"SecondViewController" bundle:nil];
if (self) {
rawUserInput = theVCMovee;
CleanerText.text = rawUserInput;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
CleanerText.text = rawUserInput;
NSLog(#"Got the other tab's text and it's %# ", rawUserInput);
}
Your code is mostly fine, but you'll find that as you have more complex view controllers that you won't necessarily want to write custom initializers to do every bit of property setting. Note that if CleanerText is a UI element that you're loading from your nib, it doesn't help to set CleanerText.text in your init method—it's not loaded until -viewDidLoad is called.
You don't have to do everything in init, though, if you declare properties for rawUserInput or other variables you want to set. You can then just go:
SecondViewController *theVCMover = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
theVCMover.rawUserInput = textView.text;
theVCMover.otherProperty = otherValue;
....
And the rest of your code works the same.
You can't (reliably) call methods on an instance until init has finished executing, so this pattern is "safe" and is how it's supposed to work.
here is my code , i am trying to get from one view to another without any memory leaks.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
firstviewcontroller *first = [[firstviewcontroller alloc] init];
[window addSubview:first.view];
[self.window makeKeyAndVisible];
return YES;
}
-(IBAction)gotosecondview:(id)sender
{
secondviewcontroller *second = [[secondviewcontroller alloc] init];
[self.view addSubview:second.view];
[second release];
}
-(IBAction)gotofirstview:(id)sender
{
[self.view removeFromSuperview];
}
to make the above code work without crashing , all i have to do is remove [second release].
if I remove it I get memory errors (build and analyze) . how can i solve this problem. and i dont want to use [self.navigationController pushViewController:second animated:YES];
all i am trying to do i navigating from one view to another and vice versa WITHOUT using navigation controller. my firstviewcontroller and secondviewcontroller are of type UIViewController.
Thanks in advance.
You need to keep the current view controller alive while its view is showing (so it can process the user input, etc.).
In your code, you can achieve that in several ways:
Keep an instance of firstviewcontroller and secondviewcontroller as instance variables, and release them on the dealloc method.
Keep an instance variable with the currently in use UIViewController and release it when you switch to another view.
The code for the second option would look something like this:
#interface
UIViewController *currentViewController;
#end
#implementation
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
firstviewcontroller *first = [[[firstviewcontroller alloc] init] autorelease];
[self switchToViewController:first];
[self.window makeKeyAndVisible];
return YES;
}
- (void)switchToViewController:(UIViewController *)aViewController {
[currentViewController.view removeFromSuperview];
[currentViewController release];
currentViewController = [aViewController retain];
[self.window addSubview:currentViewController.view];
}
-(IBAction)gotosecondview:(id)sender {
[self switchToViewController:[[[secondviewcontroller alloc] init] autorelease]];
}
#end
Here, all the logic for maintaining a single UIViewController alive lies in the switchToViewController method, which also handles the logic for switching from one view to another. As an added bonus, you can quickly add support for animations by adding a couple of lines in switchToViewController.
You can not release view in the call.
There is only one thing you can do in such conditions. use Autorelease,
The reason [second release] is crashing your code is likely because you're releasing your view controller which in turn releases the second view. The iPhone cookbook has some sample code on switching/swapping views if that's all that you're trying to accomplish. Here's the link. Hope this helps!
link text
I am having problems with getting a detail view to load using pushViewController. At first, I thought pushViewController was not working. BUT, then I tried using a different view controller and it worked. I can tell from tracing that the problem view controller is never loaded at all. In other words, I tried to eliminate the possibility that there was some error in the view controller by NSLoging in that object and I never see anything.
Does anyone have any ideas?
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
NSLog(#"hsitkjsdfkljlkd");
if (childController == nil)
childController = [[salesViewController alloc] initWithNibName:#"salesView" bundle:nil];
//NSUInteger row = [indexPath row];
[self.navigationController pushViewController:childController
animated:YES];
*/
/*
//modal = blocking
salesViewController *otherVC = [[salesViewController alloc] initWithNibName:#"salesView" bundle:nil];
//must set sale type
otherVC.strSaleType = #"Voice";
[self presentModalViewController: otherVC animated:YES];
//No close. By design, it is up to the otherVC to close itself
*/
//tipCalcViewController *detailViewController = [[tipCalcViewController alloc] initWithNibName:#"tipCalcView" bundle:nil];
salesViewController *detailViewController = [[salesViewController alloc] initWithNibName:#"salesView" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
Just Check the
- (void)viewWillAppear:(BOOL)animated
{
}
of the salesViewController.
you are doing something wrong in this..
put the debugging point in the viewWillAppear and run it. you can get the error line..
just try it......Surely it will work for u...
salesViewController *detailViewController = [[salesViewController alloc] initWithNibName:#"salesViewController" bundle:nil];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
Also make sure you are giving the IBOutlet connection to UIView.
In my case I had several IBOutlets that I removed from the Header file and forgot to remove the connection to these non-existing outlets in Interface Builder. So removing the obsolete outlets fixed the problem in my case.
When you're pushing from ViewController1 to ViewController2 then the code will be used like this so try this code,
ViewController2 *vw2=[[ViewController2 alloc]initWithNibName:#"ViewController2" bundle:nil];
[self.navigationController pushViewController:vw2 animated:YES];
The Above code may be written on Click event of button or on didSelect delegate of UITableView
I am just starting with iPhone programming. I used the utility application project template. A button on the main view of the utility application calls another view which shows some data. At the end of the presentation of the data a "end" view is shown. The "end" view contains a button to return to the main view. This all works great, however once you return to the mainview and try either to click the info button or click the button to call the data view the application terminates with the following error:
-[TheEVController startS]: unrecognized selector sent to instance 0x3933d90
EVController.m
- (IBAction)done {
EVController *controller = [[EV alloc] initWithNibName:#"MainView" bundle:nil]; //controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
MainViewController.m
- (IBAction)startS {
SVController *controller = [[SVController alloc] initWithNibName:#"SView" bundle:nil]; controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
Any suggestions?
If you want to call startS, which is a method on a MainViewController object, you can't call it on an EVController object.
[TheEVController startS];
tells the Objective-C runtime that you want to invoke the startS method in the TheEVController's class (or one of its parent classes).
It's not entirely clear from the code you posted (you didn't actually show us the section of code that is causing the exception) but from your description it sounds like you're overwriting a UIView variable that initially points to your MainViewController with a pointer to an EVController, and not resetting that variable when you show your MainViewController again.
I am trying (from my 3rd child in the heirachy) to load the root view with the following. This does not work and I get the following error when the below code is run.
-[DetailViewController clickButton:]: unrecognized selector sent to instance 0x1161e00'
Code:
MapViewController *dvController = [[MapViewController alloc] initWithNibName:#"MapView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
This exact same code works on other views, any idea how to debug this.
The code you wrote to create the MapViewController and push it onto the view controller stack is correct.
The unrecognized selector error is telling you that you are sending are attempting to call a method (named clickButton:) that does not exist.
I would suspect a spelling error. I take it that you most likely have a button defined that calls the code to create the new view. The method should look like this:
-(void) clickButton: (id) sender {
MapViewController *dvController = [[MapViewController alloc] initWithNibName:#"MapView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
I would check that you have the ":(id) sender" part. I have made the mistake before of implementing a -(void) clickButton {} method, but had the message actually sending a parameter as well.
If you have created the button programatically and set the target as clickButton, make sure the clickButton method is present
The clickButton method (according to the error) should take in an argument. So the definition of the method will be
(IBAction)clickButton:(id)sender;
If you have mapped the IBAction to an event in IB, you can omit the :(id)sender part
To load the root view controller from any view in a navigation-based application, use
[[self navigationController:popToRootViewControllerAnimated:YES];