Getting current UIViewController in Phonegap - ios

I'm loading an application (via embedded framework) via a PhoneGap Plugin.
Now, I'm trying to pass the current UIViewController to the parentViewController property of the FlashizFacade object.
When I execute my application I'm getting this error in the debug console when assigning self:
2012-09-10 13:01:43.663 gTicketSales[2805:16a03] -[FlashizPlugin navigationController]: unrecognized selector sent to instance 0x91925e0
2012-09-10 13:01:43.665 gTicketSales[2805:16a03] ***WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate:<NSInvalidArgumentException>-[FlashizPlugin navigationController]: unrecognized selector sent to instance 0x91925e0
When assigning self.viewController:
2012-09-10 14:31:21.455 gTicketSales[3693:16a03] ***WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate:<Wrong parentViewController specified>When using non-modal display, parentViewController have to be an instance of UIViewController with a valid navigationController assigned
I've tried:
facade.parentViewController = self;
facade.parentViewController = self.viewController;
My .h file
#import <Cordova/CDVPlugin.h>
#import <FlashizPaymentLib/FlashizFacade.h>
#interface FlashizPlugin : CDVPlugin <FlashizPaymentDelegate> {}
#property (nonatomic, retain) FlashizFacade* flashizFacade;
- (void) openFlashiz:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
#end
My .m file
#import "FlashizPlugin.h"
#implementation FlashizPlugin
#synthesize flashizFacade;
- (void) openFlashiz:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
NSLog(#"Flashiz Payment Opening...");
NSString *callbackId = [arguments pop];
NSString *invoiceId = [arguments objectAtIndex:0];
FlashizFacade* facade = [[FlashizFacade alloc] initWithEnvironment:FE_TEST];
facade.parentViewController = self;
facade.delegate = self;
self.flashizFacade = facade;
[facade executePaymentForInvoiceId:invoiceId];
[facade release];
CDVPluginResult *result;
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:#"Flashiz Payment Executed..."];
[self writeJavascript:[result toSuccessCallbackString:callbackId]];
}
#end
Thanks in advance!

On AppDelegate.h
#property (nonatomic, strong) UINavigationController *navigationController;
on AppDelegate.m change this line on application didFinishLaunchingWithOptions
self.window.rootViewController = self.viewController;
for this
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.navigationController = aNavigationController;
self.window.rootViewController = self.navigationController;
In my project is crashing because I don't have a ConnectionViewController, I think I didn't include the framework correctly, because I didn't know whato to do with the Resources folder, but I think it should work for you.
EDIT: I just included the resources folder, and it's working!!!
And if you don't want the navigationBar on the phonegap's view controller put in the viewDidLoad of MainViewController.m
self.navigationController.navigationBar.hidden = YES;

Related

Unrecognized selector when calling delegate Method

I'm trying to implement a SplitViewController with a NavigationController in both master and detail. I have been following this tutorial, however I'm still running into a rather strange problem.
When I try to call the delegate method I get -[UINavigationController selectedStudent:]: unrecognized selector sent to instance...
Any help would be greatly appriciated.
Here's the code:
StudentSelectionDelegate.h
#import <Foundation/Foundation.h>
#class Student;
#protocol StudentSelectionDelegate <NSObject>
#required
-(void)selectedStudent:(Student *)newStudent;
#end
StudentDetail represents detail in the split view.
In StudentDetail.h I`ve got
#import "StudentSelectionDelegate.h"
#interface StudentDetail : UITableViewController <StudentSelectionDelegate>
...
StudentDetail.m
#synthesize SentStudent;
...
-(void)selectedStudent:(Student *)newStudent
{
[self setStudent:newStudent];
}
StudentList represents master of the splitview. In StudentList.h I`ve got :
#import "StudentSelectionDelegate.h"
...
#property (nonatomic,strong) id<StudentSelectionDelegate> delegate;
In StudentList.m in the didSelectRowAtIndexPath
[self.delegate selectedStudent:SelectedStudent];
And no "SelectedStudent" is not null
And finally AppDelegate.m
#import "AppDelegate.h"
#import "StudentDetail.h"
#import "StudentListNew.h"
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *leftNavController = [splitViewController.viewControllers objectAtIndex:0];
StudentListNew *leftViewController = (StudentListNew *)[leftNavController topViewController];
StudentDetail *rightViewController = [splitViewController.viewControllers objectAtIndex:1];
leftViewController.delegate = rightViewController;
return YES;
}
P.S. I have been searching for a solution for hours.
[splitViewController.viewControllers objectAtIndex:1] is a UINavigationController, not a StudentDetail.
The error message is telling you that UINavigationController does not have a selectedStudent property.
Your delegate is not pointing to a StudentDetail but a navigation controller, which doesn't even implement < StudentSelectionDelegate>. However, since you specified the cast, Objective C can't warn you that the object you cast isn't actually the kind of class that you cast it to be.
You should consider type checking the objects, as Apple's code does, to make sure that objects are the class you expect them to be.
Here's the corrected code:
UINavigationController *rightNavController = [splitViewController.viewControllers objectAtIndex:1];
StudentDetail *rightViewController = (StudentDetail *)[rightNavController topViewController];
leftViewController.delegate = rightViewController;
As for making sure that your delegate implements the method,
if ([self.delegate respondsToSelector:#selector(selectedStudent:)]) {
[self.delegate selectedStudent:SelectedStudent];
}
would have spared you from the exception, although you'd have to have used the debugger to realize that self.delegate wasn't a StudentDetail.

Unrecognised selector sent to instance of UIViewController (or protocol)?

I'm quite new to iOS dev, I met this problem while i'm trying to embed an open source Wheel Rotating project (https://github.com/funkyboy/How-To-Create-a-Rotating-Wheel-Control-with-UIKit) into mine.
Basically, I copied the relevant code files and assets from this open source project into mine, and did some necessary changes, hope it will run.
The "Rotary Wheel Component" were from the original opensource project, and the "Mood Roulette" is my project.
Here's the screen shot of the project's structure:
And the stacktrace:
2014-03-10 13:06:12.865 Mood Roulette[13125:60b] cl is 7 | 0.392699, 0.785398, 1.178097
2014-03-10 13:06:12.866 Mood Roulette[13125:60b] -[UCViewController wheelDidChangeValue:]: unrecognized selector sent to instance 0x146d175f0
2014-03-10 13:06:12.869 Mood Roulette[13125:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UCViewController wheelDidChangeValue:]: unrecognized selector sent to instance 0x146d175f0'
*** First throw call stack:
(0x18ba1f09c 0x19799dd78 0x18ba23d14 0x18ba21a7c 0x18b9414ac 0x1000c05fc 0x1000bfb44 0x1000c26f4 0x18e8f42c0 0x18e8f4044 0x18e8fb7dc 0x18e8f8ab0 0x18e96c88c 0x18e9691f0 0x18e9629a0 0x18e8f5530 0x18e8f4720 0x18e9620b0 0x191345128 0x191344c54 0x18b9defc8 0x18b9def28 0x18b9dd14c 0x18b91db38 0x18e9612d4 0x18e95c0e8 0x1000c24b8 0x197f87aa0)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
======
The error point is on the line 100
[self.delegate wheelDidChangeValue:[self getCloveName:currentValue]];
of the code:
https://github.com/funkyboy/How-To-Create-a-Rotating-Wheel-Control-with-UIKit/blob/master/RotaryWheelProject/SMRotaryWheel.m
Here's the part I've changed (Mainly the changes were made in ViewControllers):
UCAppDelegate.h:
#import <UIKit/UIKit.h>
//#class UCViewController;
#interface UCAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
//#property (strong, nonatomic) UCViewController *viewController;
#end
UCAppDelegate.m:
#implementation UCAppDelegate
//#synthesize window;
//#synthesize viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// self.window.rootViewController = self.viewController;
// [self.window makeKeyAndVisible];
return YES;
}
UCViewController.h
#import <UIKit/UIKit.h>
#import "SMRotaryProtocol.h"
#interface UCViewController : UIViewController
#property (nonatomic, strong) UILabel *valueLabel;
#end
UCViewController.m:
#import "UCViewController.h"
#import "SMRotaryWheel.h"
#interface UCViewController ()
#end
#implementation UCViewController
#synthesize valueLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 350, 120, 30)];
valueLabel.textAlignment = NSTextAlignmentCenter;
valueLabel.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:valueLabel];
SMRotaryWheel *wheel = [[SMRotaryWheel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)
andDelegate:self
withSections:8];
wheel.center = CGPointMake(160, 240);
[self.view addSubview:wheel];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The project will build without any problems, but will leave a black screen and give a unrecognised selector error while running.
Is there anything I also need to change?
Thanks for any help.
You don't have an actual implementation of the method wheelDidChangeValue: in your UCViewController, from what you've posted. So, when that method is called at runtime, there's no method to "catch" the call, which causes the error you're seeing.
I think you have to set the delegate of the SMRotaryWheel class:
#import <UIKit/UIKit.h>
#import "SMRotaryProtocol.h"
#interface UCViewController : UIViewController <SMRotryWheelDelegate>
#property (nonatomic, strong) UILabel *valueLabel;
#end
ATTENTION: I actually don't know how the Delegate is called
And then don't forget to implement the delegate methods.

unrecognized selector sent to instance to a newly added method; messages to previously defined methods work fine

'-[MTviewFilesVC launchVF]: unrecognized selector sent to instance 0x1e59fcd0'
I added a method to a class but calling it creates 'unrecognized selector' run time error
The calling code is:
self.viewFilesVCPtr = [[MTviewFilesVC alloc] init];
[self.viewFilesVCPtr launchVF];
This works if, for example, I substitute viewDidLoad which exists already hence I
think the calling code is OK.Is there something else I need to add to the declaration of lanuchVF
to make it visible?
The method declaration, etc is:
.h:
#import "DirectoryWatcher.h"
#interface MTviewFilesVC : UITableViewController <QLPreviewControllerDataSource,
QLPreviewControllerDelegate,
DirectoryWatcherDelegate,
UIDocumentInteractionControllerDelegate>
-(IBAction)saveViewFiles;
- (void)launchVF;
#end
.m:
#interface MTviewFilesVC ()
#property (nonatomic, strong) DirectoryWatcher *docWatcher;
#property (nonatomic, strong) NSMutableArray *documentURLs;
#property (nonatomic, strong) UIDocumentInteractionController *viewFileController;
-(void) launchVF;
#end
...
- (void)lanuchVF
{
UIStoryboard *settingsStoryBoard = [UIStoryboard storyboardWithName:
#"viewFiles" bundle:nil];
UIViewController *initialViewFilesVC = [settingsStoryBoard instantiateInitialViewController];
initialViewFilesVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:initialViewFilesVC animated:YES];
}
Your method name in the .m has a typo, lanuchVF instead of launchVF :-)

objective C OSX - Delegate- unrecognized selector sent to instance

The changeView_ShowContact method will make a view controller to show up just fine. But when I call the changeView_ShowContact from another class it wont work. Im getting an error in the log "unrecognized selector sent to instance."
#protocol callingActions_fromMainNavigation <NSObject>
- (IBAction)changeView_ShowContact:(id)sender;
#end
**#interface Navigation_Main : NSViewController**
#property (nonatomic, strong) id <callingActions_fromMainNavigation> delegate;
**#implementation Navigation_Main**
#synthesize delegate;
- (IBAction)changeView_ShowContact:(id)sender;
{
NSLog(#"********************ShowContact");
AddStuffViewController = [[pageContact alloc] initWithNibName:#"pageContact" bundle:nil];
[AddStuffView addSubview:[AddStuffViewController view]]; //<This call here works ok.
}
.
#interface contacts : NSObject <callingActions_fromMainNavigation>
**#implementation contacts**
-(void)myMethodCall:(id)sender;
{
Navigation_Main *NavMain = [[Navigation_Main alloc] initWithNibName:#"Navigation_Main.h" bundle:nil];
[NavMain setDelegate:self];
[self changeView_ShowContact:nil];
//I need to call the (IBAction)changeView_ShowContact in the Main Navigation. This
//code is not working.
}
It looks like you are having your contacts adopt the callingActions_fromMainNavigation protocol with out implementing changeView_ShowContact: in the contacts class.

Unrecognized selector sent to instance using Storyboards

I'm using storyboard for an iOS application, my storyboard looks like this: http://d.pr/7yAY (droplr url)
The problem is when I click the login button, I send the username captured to the events table view controller. To do that I use prepareForSegue function, but apparently when I try to set the username in throws an exception.
My code is as follows:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
- (IBAction) logintButton:(id)sender;
#property (weak, nonatomic) IBOutlet UITextField *username_tf; // textfield
#end
ViewController.m
#import "ViewController.h"
#import "EventsTableViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize username_tf;
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"ToMainApp"])
{
EventsTableViewController * dest = (EventsTableViewController *)[segue destinationViewController];
NSString * username = [[NSString alloc] initWithFormat:#"%#", username_tf.text];
[dest setUsername:username];
}
}
- (IBAction) logintButton:(id)sender
{
//NSLog(#"Logint button pressed");
[self performSegueWithIdentifier:#"ToMainApp" sender:sender];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setUsername_tf:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
EventsTableViewController.h
#import <UIKit/UIKit.h>
#interface EventsTableViewController : UITableViewController
{
NSString * username;
}
#property (nonatomic, retain) NSString * username;
#end
EventsTableViewController.m
#import "EventsTableViewController.h"
#interface EventsTableViewController ()
#end
#implementation EventsTableViewController
#synthesize username;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
...
#end
The throwed exception is:
2012-03-15 14:19:27.304 StoryboardAssistance[30989:f803]
-[UINavigationController setUsername:]: unrecognized selector sent to instance 0x68abf60 2012-03-15 14:19:27.306
StoryboardAssistance[30989:f803] *** Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason:
'-[UINavigationController setUsername:]: unrecognized selector sent to
instance 0x68abf60'
*** First throw call stack: (0x13c9022 0x155acd6 0x13cacbd 0x132fed0 0x132fcb2 0x28e6 0x43e4be 0xdb5ab 0x2974 0x13cae99 0x1614e 0x160e6
0xbcade 0xbcfa7 0xbc266 0x3b3c0 0x3b5e6 0x21dc4 0x15634 0x12b3ef5
0x139d195 0x1301ff2 0x13008da 0x12ffd84 0x12ffc9b 0x12b27d8 0x12b288a
0x13626 0x253d 0x24a5) terminate called throwing an exception(lldb)
Any suggestions?
Your segue's destination view controller is your Navigation Controller, not your Events Table View controller.
You can get the Events controller by accessing the Navigation controller's topViewController property.
Try this:
UINavigationController *navController = (UINavigationController*)[segue destinationViewController];
EventsTableViewController *eventsController = [navController topViewController];
NSString * username = [[NSString alloc] initWithFormat:#"%#", username_tf.text];
[eventsController setUsername:username];
Alternatively, try this:-
(It does the same as jonkroll's answer but in one line and removes the warning "Incompatible pointer types initializing 'ViewController *__strong' with an expression of type UIViewController *"
NameOfViewController *vc = (NameOfViewController *)[[segue destinationViewController] topViewController];
Another thing to verify is that you have properly assigned your destination ViewController as the proper class in Interface Builder. By default this will be UIViewController or UITableViewController etc. If you have a custom class with getters/setters, you need to remember to change the class in the Interface Builder to reflect accordingly, otherwise you will receive an invalid selector error message.

Resources