How to have a single IB Outlet for 2 UIViews - ios

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

Related

FBAudienceNetwork: setting FBNativeAd into FBMediaView stacks the UI

The following line of code makes my UI stack
adMediaView.nativeAd = nativeAd
// adMediaView - FBMediaView
// nativeAd - FBNativeAd
I've tried putting it in the performing it asynchronously in background thread but it didn't help. Is there a way to fix it?
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),{
adMediaView.nativeAd = nativeAd
});
I've installed FBAudienceNetwork via pods and updated it just now. The latest version I have is 4.7.0
pod 'FBAudienceNetwork'
NativeAdView uses FBMediaView for creating an ad.
Now, in your View Controller header file declare FBNativeAdDelegate protocol as well as declare and connect instance variables to your UI .XIB:
#import FBAudienceNetwork; // import Audience Network module
#interface MyViewController : UIViewController <FBNativeAdDelegate>
// Other code might go here...
#property (weak, nonatomic) IBOutlet UIImageView *adIconImageView;
#property (weak, nonatomic) IBOutlet UILabel *adTitleLabel;
#property (weak, nonatomic) IBOutlet UILabel *adBodyLabel;
#property (weak, nonatomic) IBOutlet UIButton *adCallToActionButton;
#property (weak, nonatomic) IBOutlet UILabel *adSocialContextLabel;
#property (weak, nonatomic) IBOutlet UILabel *sponsoredLabel;
#property (weak, nonatomic) FBMediaView *adCoverMediaView;
#property (weak, nonatomic) IBOutlet UIView *adView;
#end
Then, add a method in your View Controller's implementation file that initializes FBNativeAd and request an ad to load:
FBNativeAd *nativeAd;
FBAdchoicesView *adChoicesView;
- (void)showNativeAd
{
nativeAd = [[FBNativeAd alloc] initWithPlacementID:#"YOUR_PLACEMENT_ID"];
nativeAd.delegate = self;
[nativeAd loadAd];
}
Now that you have added the code to load the ad, add the following functions to handle loading failures and to construct the ad once it has loaded:
- (void)nativeAdDidLoad:(FBNativeAd *)nativeAd
{
[self.adTitleLabel setText:nativeAd.title];
[self.adBodyLabel setText:nativeAd.body];
[self.SocialContextLabel setText:nativeAd.socialContext];
[self.sponsoredLabel setText:#”Sponsored”];
[self.adCallToActionButton setTitle:nativeAd.callToAction];
[nativeAd.icon loadImageAsyncWithBlock:^(UIImage *image) {
[self.adIconImageView setImage:image];
}];
// Allocate a FBMediaView to contain the cover image or native video asset
adCoverMediaView = [[FBMediaView alloc] initWithFrame:coverFrame]];
[adCoverMediaView setNativeAd:nativeAd];
// Add adChoicesView
adChoicesView = [[FBAdChoicesView alloc] initWithNativeAd:nativeAd];
[adView addSubview:adChoicesView];
[adChoicesView updateFrameFromSuperview];
// Register the native ad view and its view controller with the native ad instance
[nativeAd registerViewForInteraction:adView withViewController:self];
}
- (void)nativeAd:(FBNativeAd *)nativeAd didFailWithError:(NSError *)error
{
NSLog(#"Ad failed to load with error: %#", error);
}
For displaying the native ad cover image, it is recommended to use the Facebook Audience Network MediaView which can display both image and video assets.
Reference : https://developers.facebook.com/docs/audience-network/ios/native-api

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!

iOS Instantiate Custom View from Nib File across multiple UIViewController

I need an example of how to instantiate a custom view from a nib file. i wrapped my head around several stackoverflow posts but i didn't get it. Maybe i only need a hint how to do it.
what I want to do is - creating a custom view with interface builder. it should be a template so that i can instantiate it multiple times to add it to multiple view controllers.
So far I have created my custom view xib file called MyIssueView.xib. Which consists in fact just of the main view and some labels.
And I created a Subclass of UIView which is called IssueView with the Outlets for my labels in the MyIssueView.xib.
How do I now hook up the outlets with the IB? And how can I instantiate the IssueView from any ViewController?
Would be glad for an example!
Cheers.
UPDATE:
I now have
IssueView.xib
IssueView.h (UIView Subclass)
IssueView.m
My IssueView.h:
#import <UIKit/UIKit.h>
#interface IssueView : UIView
#property (weak, nonatomic) IBOutlet UILabel *label1;
#property (weak, nonatomic) IBOutlet UILabel *label2;
#property (weak, nonatomic) IBOutlet UILabel *label3;
#property (weak, nonatomic) IBOutlet UILabel *label4;
#end
My IssueView.m:
#import "IssueView.h"
#implementation IssueView
#end
My ViewController.m:
#import "AllIssuesViewController1.h"
#import "IssueView.h"
#import "UIView+NibLoading.h"
#interface AllIssuesViewController1 ()
#end
#implementation AllIssuesViewController1
- (void) loadView
{
}
- (void)viewDidLoad
{
[super viewDidLoad];
_issueView = [IssueView loadInstanceFromNib];
}
It gives me an error:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x8292580> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label1.'
I have a category that extends UIView called UIView+NibLoading.
In it is this code...
#import "UIView+NibLoading.h"
#implementation UIView (NibLoading)
+ (id)loadInstanceFromNib
{
UIView *result;
NSArray* elements = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil];
for (id anObject in elements) {
if ([anObject isKindOfClass:[self class]]) {
result = anObject;
break;
}
}
return result;
}
#end
Then when I want to instantiate a UIView subclass from a nib I just import the category and I can run...
self.issueView = [IssueView loadInstanceFromNib];\
You need to connect the labels like this...
If you connecting the labels to "File's Owner" then you'll need to remove those connections.

Declared Interface errors when building the project

I im fairly new to ios development, and following the tutorial exercise on chapter 4 of the book 'beginning ios5 development' i have run into some compile errors on my code.
here's what my header code .h file looks like
#import <UIKit/UIKit.h>
#interface BIDViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *namefIELD;
#property (strong, nonatomic) IBOutlet UITextField *numberField;
#property (strong, nonatomic) IBOutlet UILabel *sliderLabel;
#property (strong, nonatomic) IBOutlet UISegmentedControl *leftSwitch;
#property (strong, nonatomic) IBOutlet UISegmentedControl *rightSwitch;
#property (strong, nonatomic) IBOutlet UIButton *doSomethingButton;
-(IBAction)textFieldDoneEditing:(id)sender;
//to initialize the 'done' button when editing to confirm you have finished editing/
-(IBAction) backgroundTap:(id)sender;
- (IBAction)sliderChanged:(id)sender;
- (IBAction)switchChanged:(id)sender;
- (IBAction)toggleControls:(id)sender;
- (IBAction)buttonPressed:(id)sender;
#end
and here's my .m code where im facing the error "no visible #interface for 'UISegmentedControl' declares the selector 'setOn:animated;'
#synthesize leftSwitch;
#synthesize rightSwitch;
- (IBAction)switchChanged:(id)sender {
UISwitch *whichSwitch = (UISwitch *)sender;
BOOL setting = whichSwitch.isOn;
[leftSwitch setOn: setting animated: YES]; //the error lies here/
[rightSwitch setO: setting animated: YES]; //the error lies here/
}
- (IBAction)toggleControls:(id)sender {
}
- (IBAction)buttonPressed:(id)sender {
}
#end
the error statement is "no visible #interface for 'UISegmentedControl' declares the selector 'setOn:animated;'
pls help, that would be highly appreciated and voted for :)
#PhilipMills ...here's what i'm talking about, as soon as i click on a leftside or rightside of the segmented control object a break point is automatically made on my delegated header code here
#import <UIKit/UIKit.h>
#import "BIDAppDelegate.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([BIDAppDelegate class])); //the breakpoint appears here.
}
}
A UISwitch has a setOn:animated: method but a UISegmentedControl does not. I'll guess that you assigned objects to leftSwitch and rightSwitch that are not the kind you intended (or the tutorial intended).

Set title of a button there is selected from an array - Xcode iOS

I have a lot of buttons that i would like to give a title when the view is loaded.
The title of the buttons varies depending on what day of the month it is. Therefore I have created an array with the name of all the buttons. My code is as follows:
My interface:
#property (strong, nonatomic) IBOutlet UIButton *button1;
#property (strong, nonatomic) IBOutlet UIButton *button2;
#property (strong, nonatomic) IBOutlet UIButton *button3;
#property (strong, nonatomic) IBOutlet UIButton *button4;
#property (strong, nonatomic) IBOutlet UIButton *button5;
#property (strong, nonatomic) IBOutlet UIButton *button6;
#property (strong, nonatomic) IBOutlet UIButton *button7;
#property (strong, nonatomic) IBOutlet UIButton *button8;
#property (strong, nonatomic) IBOutlet UIButton *button9;
#property (strong, nonatomic) IBOutlet UIButton *button10;
My implementation:
#synthesize button1 = _button1;
#synthesize button2 = _button2;
#synthesize button3 = _button3;
#synthesize button4 = _button4;
#synthesize button5 = _button5;
#synthesize button6 = _button6;
#synthesize button7 = _button7;
#synthesize button8 = _button8;
#synthesize button9 = _button9;
#synthesize button10 = _button10;
NSArray *myArray = [NSArray arrayWithObjects: #"dummyButton", #"_button1", #"_button2", #"_button3", #"_button4", #"_button5", #"_button6", #"_button7", #"_button8", #"_button9", #"_button10", nil];
for (int i = varDefinedEarlier; i<=totalAmountOfNeededTitles; i++) {
NSString *theTitleSting = [NSString stringWithFormat:#"%i",i];
NSLog(#"arrayValue:%#", [myArray objectAtIndex:i]); //Works prints the button name (for example _button7)
[[myArray objectAtIndex:i] setTitle:theTitleSting forState:UIControlStateNormal];
//The line above is where the app crashes. Everything else works just fine.
}
The error provided to me is a "Thread 1: signal SIGABRT
Besides that, this is what the log says:
-[__NSCFConstantString setTitle:forState:]: unrecognized selector sent to instance 0x14a48
2012-07-10 22:57:49.649 Don't Break the Chain[10302:707] * * * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString setTitle:forState:]: unrecognized selector sent to instance 0x14a48'
* * * First throw call stack:
(0x3774788f 0x3544c259 0x3774aa9b 0x37749915 0x376a4650 0xdc2f 0x31454c8b 0x314611e9 0x31461059 0x31460f3f 0x3146070b 0x31460503 0x31454aff 0x314547d5 0x314cd903 0x31547627 0x37fb8933 0x3771ba33 0x3771b699 0x3771a26f 0x3769d4a5 0x3769d36d 0x33e5c439 0x31449cd5 0xb7cf 0xb774)
terminate called throwing an exception(lldb)
I think the error is the way i try to tell the app witch button it needs to set a title to, but I can not really figure out how to get it to take on the value of my array and use it as the button name.
I'm pretty new in iPhone iOS development, so bear with me if the answer is simple ;)
You are not referencing the button instances but some strings within that array. Now within your loop, you are trying to invoke the method setTitle:forState: on those strings which does not exist for NSString instances.
Try the following:
NSArray *myArray = [NSArray arrayWithObjects:_button1, _button2, _button3, _button4, _button5, _button6, _button7, _button8, _button9, _button10, nil];
This will place the actual button instances into your array, as intended and your code should work fine.
you should do something like [myButton setTitle:[myArray objectAtIndex:i] forState: UIControlStateNormal]
at the moment you are passing the setTitle message to a string type which - of course - should result in a crash...
hope it helps

Resources