Unable to set property of a UIViewController ios - ios

I have a view controller which extends UIViewController.
#interface MyViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextView *phoneTxt;
#property (nonatomic, strong) NSDictionary *data;
#end
However when I try to set the property of the view controller by:
MyViewController *vvc = (MyViewController *)[sb instantiateViewControllerWithIdentifier:#"MyVC"];
vvc.data = data;
I get following error:
'NSInvalidArgumentException', reason: '-[UIViewController setData:]: unrecognized selector sent to instance
I have checked, the view controller is not nil and I have synthesized the property data in MyViewController.m
#synthesize data = _data;
Why am I getting this error?

I think you probably forgot to set the proper class for your view controller (File's Owner) in your storyboard/xib, so it probably gets instantiated as a raw UIWievController.

Related

How to have a single IB Outlet for 2 UIViews

I'm currently developing an application that displays ads. The main view controller has 2 UIViews that act as containers for the ad views that will be passed in when they are loaded. However, I'm using an API that only takes a single "adContainer" as a parameter, so tried using an IBOutlet Collection called adContainer that has each of these 2 UIViews (adContainer1 and adContainer2) linked to it. I tagged each of these so that on the main view controller when I try to load an ad, I can iterate through the adContainers in the IBOutlet Collection and request each adContainer individually based on the object's tag. I'm being thrown this error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance
Here is the code for my viewController.h :
#import <UIKit/UIKit.h>
#import "MPAdView.h"
#import "MPViewController.h"
#class MPAdInfo;
#interface MPSmaatoScrollBannerAdDetailViewController :
UIViewController <MPAdViewDelegate>
#property (weak, nonatomic) IBOutlet UILabel *titleLabel;
#property (weak, nonatomic) IBOutlet UILabel *IDLabel;
#property (weak, nonatomic) IBOutlet UILabel *failLabel;
#property (weak, nonatomic) IBOutlet UIActivityIndicatorView *spinner;
#property (strong, nonatomic) IBOutletCollection(UIView) NSArray
*adViewContainer;
- (id)initWithAdInfo:(MPAdInfo *)info;
#end
And here is the function in my viewController.m that I suspect is throwing the error:
- (void)configureAd
{
for (UIView *ad in _adViewContainer) {
if (ad.tag == 1) {
self.adView1 = [[MPSampleAppInstanceProvider sharedProvider] buildMPAdViewWithAdUnitID:self.info.ID
size:ad.bounds.size];
self.adView1.delegate = self;
self.adView1.accessibilityLabel = #"smaato banner";
self.adView1.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[ad addSubview:self.adView1];
[self.adView1 stopAutomaticallyRefreshingContents];
} else if (ad.tag == 2) {
self.adView2 = [[MPSampleAppInstanceProvider sharedProvider] buildMPAdViewWithAdUnitID:self.info.ID
size:ad.bounds.size];
self.adView2.delegate = self;
self.adView2.accessibilityLabel = #"smaato banner";
self.adView2.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[ad addSubview:self.adView2];
[self.adView2 stopAutomaticallyRefreshingContents];
}
}
}
Please help! Thank you in advance!
According to the way you said, I did not report an error myself; it may be that your xib cable is not set correctly.
You can refer to this:
"countByEnumeratingWithState:objects:count:" Error

iOS: Detect backspace in empty UITextField with UITextField subclass

The clear answer to detecting a backspace is by creating a subclass of UITextField and overriding the deleteBackward property.
I've created a subclass a UITextField subclass, but am getting this error:
'NSInvalidArgumentException', reason: '-[UITextField setMyDelegate:]: unrecognized selector sent to instance
This is my subclass code:
My TextField.h:
#protocol MyTextFieldDelegate <NSObject>
#optional
- (void)textFieldDidDelete;
#end
#interface MyTextField : UITextField<UIKeyInput>
//create "myDelegate"
#property (nonatomic, assign) id<MyTextFieldDelegate> myDelegate;
#end
My TextField.m:
- (void)deleteBackward {
[super deleteBackward];
if ([_myDelegate respondsToSelector:#selector(textFieldDidDelete)]){
[_myDelegate textFieldDidDelete];
}
}
In my ViewController that I would like to access the UITextField subclass I do the following:
#import "MyTextField.h"
<MyTextFieldDelegate>
#property (nonatomic, strong) IBOutlet MyTextField *passcode1;
self.passcode1.myDelegate = self;
Any ideas as to why I am getting the unrecognized selector error? It looks to me like I have done everything correctly in subclassing UITextField.
The problem is with your outlet. The property is defined with your custom class but in a Interface Builder you added a plain old UITextField. Hence the error at runtime. In Interface Buldier, update the text field's class to be your custom class.

FBLogin assign delegate from UIView

I have a FBLogin (Facebook SDK) button I am using in a view being replicated throughout many of my view controllers. It seems I cannot set a UIView as a delegate to the Facebook SDK, so I am trying to create a seperate ViewController to handle the delegate functions of the Facebook SDK.
Header file:
#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
#class AppDelegate, FBDelegateViewController;
#interface UserView : UIView {
AppDelegate *appDelegate;
IBOutlet UIView *contentView;
IBOutlet UIImageView *userImage;
IBOutlet UILabel *firstName;
FBDelegateViewController *fbDelegate;
IBOutlet FBLoginView *FacebookLogin;
}
#property (nonatomic, strong) IBOutlet UIView *contentView;
#property (nonatomic, strong) IBOutlet UILabel *firstName;
#property (nonatomic, strong) AppDelegate *appDelegate;
#property (nonatomic, strong) FBDelegateViewController *fbDelegate;
Currently in my implementation I am doing the following:
#import "UserView.h"
#import "AppDelegate.h"
#import "FBDelegateViewController.h"
#import <FacebookSDK/FacebookSDK.h>
#implementation UserView
#synthesize contentView, firstName, appDelegate, fbDelegate;
- (void)awakeFromNib {
fbDelegate = [[FBDelegateViewController alloc] init];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[NSBundle mainBundle] loadNibNamed:#"UserView" owner:self options:nil];
[self addSubview:self.contentView];
[userImage.layer setCornerRadius:21.5];
[FacebookLogin setDelegate:fbDelegate];
}
#end
The application crashes with the following:
** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setDelegate:]: unrecognized selector sent to instance 0xa5e4f80'*
In interface builder the UIView that contains the FBLogin item is set.
I also tried delegation with the UIView as well, but it looks like a UIView cannot be a delegate for a FBLogin?
Sorry, new territory here. Typically have used the associated view controller as a delegate - but want to understand external delegation.
It seems I cannot set a UIView as a delegate to the Facebook SDK, so I
am trying to create a seperate ViewController to handle the delegate
functions of the Facebook SDK.
This assumption is false, any NSObject can be set as the delegate as long as it implements the FBLoginViewDelegate protocol
This can be done like this:
#interface UserView : UIView <FBLoginViewDelegate> {
After you do this, you can set this view as the delegate!

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 :-)

DetailViewController with UIView causes "Unrecognized Selector" error when selecting row in table

I have a table view with a list of categories that I would like the user to be able to select in order to rate. I've created a separate UIView (RateView) to manage the ratings within the detailviewcontroller.
I'm able to get the ratings to work fine when developing independently of the project and without a table. However, when incorporating the code for tracking ratings into storyboard and with a table, I receive the following error when attempting to navigate from the table to the detail view:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setNotSelectedImage:]: unrecognized selector sent to instance 0xad9b280' (notSelectedImage is a property of the UIView titled RateView).
Below are the interface files for each of the referenced view controllers.
* RatingsList_ViewController *
#import <UIKit/UIKit.h>
#import "Rate_ViewController.h"
#interface RatingsList_ViewController : UITableViewController
- (IBAction)dismissModal:(id)sender;
#end
Here is the SEGUE within the table to Rate_ViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"toRateDetail" sender:self];
Rate_ViewController *rateCategory;
rateCategory = (Rate_ViewController *) self.navigationController;
}
* Rate_ViewController *
#import <UIKit/UIKit.h>
#import "RateView.h"
#interface Rate_ViewController : UIViewController <UITextFieldDelegate, RateViewDelegate>
#property (weak, nonatomic) IBOutlet RateView *rateView;
#property (weak, nonatomic) IBOutlet UILabel *statusLabel;
#property (weak, nonatomic) IBOutlet UITextField *notesField;
#end
I suspect that I've not delegated properly but any assistance you could provide would be most appreciated.
Thanks in advance!

Resources