Calling View from Class - iphone

I think I have a simple issue with my app.
First of all I am using the PraseSDK in order to use the LoginView they offer.
Now I was going to create that function and let it called by a view controller in
-(void) viewDidLoad
It worked perfectly.
Now I was wondering if I can put that code into a global function class?
Well I created a Class called: glo_function
Inside of it I created a function which is call
+(void) CallLoginScreen{
PFLogInViewController *login = [[PFLogInViewController alloc] init];
login.fields = PFLogInFieldsUsernameAndPassword | PFLogInFieldsLogInButton;
[self presentModalViewController:login animated:NO];}
In My ViewController Iam using "callLoginScreen" like this
[glo_function CallLoginScreen]
The methode is going to be called by the View Controller but the view will not show up.
Well I get that error will trying to run the app.
No known class method for selector 'presentModalViewController:animated:'
So I am pretty sure It do to the fact that I used the "self" in->
[self presentModalViewController:login animated:NO]
Can someone help me out with it? should be easy, hopefully :)
-----------------------Response------------------------------
Hi after I did that, the app crashes.
The Call methode looks like that:
[glo_function showLoginScreenUsingViewController];
In glo_function.m the methode looks like that:
+ (void)showLoginScreenUsingViewController:(UIViewController *)VC {
PFLogInViewController *login = [[PFLogInViewController alloc] init];
login.fields = PFLogInFieldsUsernameAndPassword | PFLogInFieldsLogInButton;
[VC presentModalViewController:login animated:NO];}
The Result when starting the App:
2012-12-28 21:20:24.003 Logbuch40[1942:c07]
*** Terminating app due to uncaught exception 'NSInvalidArgumentException'
, reason: '+[glo_function showLoginScreenUsingViewController]:
unrecognized selector sent to class 0x22e9ec

"self" always refers to the object you are in. In your case it's the glo_function object. So when you say self you are talking to the wrong object.
You could still do what you want but in your glo_function method you need to pass in a reference to your view controller. Then just use that in your function instead of self.
+ (void)showLoginScreenUsingViewController:(UIViewController *)VC {
PFLogInViewController *login = [[PFLogInViewController alloc] init];
login.fields = PFLogInFieldsUsernameAndPassword | PFLogInFieldsLogInButton;
[VC presentModalViewController:login animated:NO];
}

Related

Cannot push TTTableViewController onto Navigation Controller

I have an app where I have a TTTableView Controller inside a Navigation Controller that is Insider a TabBar.
I want it so that if a user selects an item it will push another TTTableView with the items under that category.
The code I have is
-(void)didSelectObject:(id)object atIndexPath:(NSIndexPath *)indexPath {
if ([object isKindOfClass:[TTTableMoreButton class]]) {
[super didSelectObject:object atIndexPath:indexPath];
} else {
CategoryViewController *viewController = [[CategoryViewController alloc] initWithNibName:#"CategoryViewController" bundle:nil];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
}
}
The CategoryViewController is setup as
#interface CategoryViewController : TTTableViewController
and the CategoryViewController.xib file has the datasource & delegate set to the files owner and the view set to the tableview and the tableview class is set to TTTableView.
When I run it I get the following error when selecting a row
2011-10-17 16:18:23.819 Biz Insider[34067:f803] -[CategoryViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x6c93d30
2011-10-17 16:18:23.820 Biz Insider[34067:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CategoryViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x6c93d30'
*** First throw call stack:
(0x1893052 0x157ed0a 0x1894ced 0x17f9f00 0x17f9ce2 0xb4cf2b 0xb4f722 0x9ff7c7 0x9ff2c1 0x9b61e 0xa0228c 0xa06783 0x9bb48 0x9b1301 0x1894e72 0x89192d 0x89b827 0x821fa7 0x823ea6 0x823580 0x18679ce 0x17fe670 0x17ca4f6 0x17c9db4 0x17c9ccb 0x1f88879 0x1f8893e 0x972a9b 0x273d 0x26b5)
terminate called throwing an exceptionCurrent language: auto; currently objective-c
If I try and push another view (i have one with a webview on it) then it works fine, or if I go into interface builder and link the File Owner's "tableView" to the TTTableView object it will work fine and push the controller except the "Pull down to refresh" function wont work so I am assuming that the deletage isn't correct when doing it that way.
Any help would be greatly appreciated.
EDIT:
I have a feeling that it has something to do with the following
-(id<UITableViewDelegate>)createDelegate {
return [[[TTTableViewDragRefreshDelegate alloc] initWithController:self] autorelease];
}
This sets the delegate to TTTableViewDragRefreshDelegate which implements the numberOfRowsInSection and all that junk. Is there another way to do this?
Cheers,
Dean
The log says that in your custom class CategoryViewController method tableView:numberOfRowsInSection: is not implemented and so it couldn't call it.
Check it and check types of its parameters.
Well this is an odd one, but I changed it from
CategoryViewController *viewController = [[CategoryViewController alloc] initWithNibName:#"CategoryViewController" bundle:nil];
to
CategoryViewController *viewController = [[CategoryViewController alloc] init];
and it magically works now...dont ask me how or why.
There must have been something in the NIB that was messing it up, but I dont know how the whole layout is working now with no NIB.

presentModalViewController:animated not found

In the app I am trying to build, I am trying to use the UIImageController but am running into a bit of a problem. I am not doing this in the ViewController class... Here's the code:
(IBAction) uploadBtn {
ipc = [[UIImagePickerController alloc] init];
ipc.delegate = self;
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:ipc animated:YES];'
Its giving me a warning with the last line, saying:
Method'- presentModalViewController:animated' not found
I am sure that if I used the ViewController class this would work but is there anything I can do/import that will let me do this in just a UIView class?
Again the app builds, but when I try to click the button it crashes
Thanks in advance!
You're crashing because only UIViewController has that method. You can only present one view controller off of another one.
Have a look at get UIView's viewcontroller (iphone). I think you should rethink your design if you need to access view controller from a UIView.
You could present it from the view's parent view controller:
[[ipc parentViewController] presentModalViewController:yourController animated:yourBool];

iPhone - unrecognized selector sent to instance - Trying to open the starting view after viewing several other views

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.

iPhone SDK: Switching to one view then back to previous view errors

I have a UITabBarConroller that I use to switch between 3 different views. This all works perfectly. On one of my tabs, I added a button at the to called "Add", I have added an outlet to this, as well as an IBAction method which looks like the following:
// Method used to load up view where we can add a new ride
- (IBAction)showAddNewRideView {
MyRidesViewController *controller = [[MyRidesViewController alloc] initWithNibName:#"AddNewRide" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controller animated:YES];
[controller release];
}//end showAddNewRideView
This currently works fine, and loads up my AddNewRide nib file. But, once that view loads, I have a cancel button, which, when clicked, I want to return to the previous view. So, I figured I would just do the reverse of the above, using the following method which would load back my previous nib:
- (IBAction)cancelAddingNewRide {
MyRidesViewController *controller = [[MyRidesViewController alloc] initWithNibName:#"MainWindow" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controller animated:YES];
[controller release];
}//end cancelAddingNewRide
But, which trying to load the MainWindow nib, the program crashes, and I get the following error:
2010-05-05 20:24:37.211 Ride[6032:207] *** -[MyRidesViewController cancelAddingNewRide]: unrecognized selector sent to instance 0x501e450
2010-05-05 20:24:37.213 Ride[6032:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[MyRidesViewController cancelAddingNewRide]: unrecognized selector sent to instance 0x501e450'
So, I am a little lost as to why it would work one way, but not the other.
First, I wanted to address part of the error: Think of your views as a stack. When you "push" a modal controller, you are adding that view to a stack. The old view is still there underneath. So you need to "pop" off the modal view to return to the old view. If you push a new view on, you now have 3 views on the stack which are all taking up memory, where you really only need one.
So, inside cancelAddingNewRide just try:
[super dismissModalViewControllerAnimated:true];
You may have other issues that are causing the crash, but this should generally get things working.
Typically when I have used presentModalViewController the presented viewController tells the calling viewController to dismiss it using dismissModalViewControllerAnimated:YES;
So in other words in the cacncelAddingNewRide you simply call the class that hass showAddnewRideView in it and have it pass itself to the method.
It's hard to explain but I'll show you an example:
cancelAddingNewRide class:
- (IBACtion)home:(id)sender {
if (self.delegate respondsToSelctor:#selector(dismiss:)]) {
[self.delegate dismiss:self];
}
}
and then in the showAddNewRideView class
-(void) dismiss:(cancelAddingNewRide_class *) controller {
[self dismissModalViewControllerAnimated:Yes];
}
Hope that makes sense and soz for typos
Edit: oh and make the controller's delegate self
controller.delegate = self;
Actually thinking about it more there is a bit more to this. You have to define the called viewController as a Delegate. Have a look at Stanford universities iPhone lectures, lecture 11 deals with this and is available from iTunesU.

pushViewController debugging

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];