ios UiPickerView with CountryPicker class - ios

Im trying to add the UiPickerView class from https://github.com/nicklockwood/CountryPicker
I want to use with storyboard in my viewController but the delegate method is never getting called although the pickerView shows the countries.
Here is what ive done:
ive imported the CountryPicker folder and Flags folder, in the storyboard controller ive added a UiPickerView and assign it to CountryPicker class.
The ViewController.h and .m files are exactly the same as the example.
i cant find what im missing.
Also i tried init the object and call the setSelectedCountry method from my ViewController.m file but its also not doing anything.
ViewController.h
#import <UIKit/UIKit.h>
#import "CountryPicker.h"
#interface ViewController : UIViewController <CountryPickerDelegate>
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
CountryPicker *picker = [[CountryPicker alloc] init];
[picker.delegate self];
[picker setSelectedCountryCode:#"US" animated:YES];
}
- (void)countryPicker:(__unused CountryPicker *)picker didSelectCountryWithName:(NSString *)name code:(NSString *)code
{
NSLog(#"country: %#",name);
}
#end

You need to create an IBOutlet from the storyboard to your code Apple Docs
Remove the code you have in your viewDidLoad that creates a new picker.
Then for the the new IBOutlet you've created, set self as the delegate, so if your outlet was called picker your viewDidLoad should look as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
self.picker.delegate = self;
[self.picker setSelectedCountryCode:#"US" animated:YES];
}

Related

delegate respondsToSelector:selector not working

I saw a lot of this kind of questions and answers here, but couldn't find solution to my problem. I'm trying to send data from one view controller to another and use delegate. But don't know why my postDelegate doesn't responds to selector. Is something wrong with this code or what is the problem?
PostViewController.h file
#protocol GetDataDelegate <NSObject>
-(void)getPassedInfo:(NSString*)info;
#end
#interface PostViewController : UIViewController
#property (nonatomic, weak) id <GetDataDelegate> postDelegate;
#end;
PostViewController.m file
#import "PostViewController.h"
- (IBAction)postData:(id)sender {
if ([_postDelegate respondsToSelector:#selector(getPassedInfo:)]) {
[self.postDelegate getPassedInfo:#"data"];
NSLog(#"responds");
}
[self dismissViewControllerAnimated:YES completion:nil];
}
in second view controllers .h file
#import "PostViewController.h"
#interface MainViewController : UITableViewController <GetDataDelegate>
and in .m file
#implementation MainWindowTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
PostViewController * postController = [[PostViewController alloc]init];
postController.postDelegate = self;
}
and here is delegate method:
-(void)getPassedInfo:(NSString *)info{
NSLog(#"info is %#", info);
}
You need to make postController a property or an ivar. Currently it is a local variable in the viewDidLoad method which will be deallocated after viewDidLoad completes as #CodaFi said above.
#import "PostViewController.h"
#interface MainViewController : UITableViewController <GetDataDelegate>
#property (nonatomic, strong) PostViewController *postController;
#end
Then:
- (void)viewDidLoad
{
[super viewDidLoad];
self.postController = [[PostViewController alloc]init];
self.postController.postDelegate = self;
}

Delegate method not being called, setting delegate to self?

So I'm trying to get a hang of using delegates, and I've watched a few tutorials on how to use them so far. I still find them confusing and after trying to implement one myself, have an issue that I can't seem to solve.
I have two ViewControllers, the first one ViewController contains a UITextField *sampleTextField and a button with the method switchViews. It also contains the protocol declaration with the method sendTextToViewController. SwitchViews is also linked to a segue that switches to the SecondViewController. In SecondViewController the only object is a UILabel *outputLabel When the user taps the button, it calls switchViews and the view changes to SecondViewController, and upon loading outputLabel should be changed to whatever text was entered in sampleTextField in ViewController. However the delegate method sendTextToViewController is never being called. All objects are created in Interface Builder.
Here is the code to make it a bit easier to understand:
ViewController.h
#import <UIKit/UIKit.h>
#protocol TextDelegate <NSObject>
-(void)sendTextToViewController:(NSString *)stringText;
#end
#interface ViewController : UIViewController
- (IBAction)switchViews:(id)sender;
#property (weak, nonatomic) IBOutlet UITextField *sampleTextField;
#property (weak, nonatomic) id<TextDelegate>delegate;
#end
Then declared this in ViewController.m
- (IBAction)switchViews:(id)sender {
NSLog(#"%#", self.sampleTextField.text);
[self.delegate sendTextToViewController:self.sampleTextField.text];
}
SecondViewController.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
#interface SecondViewController : UIViewController <TextDelegate>
#property (weak, nonatomic) IBOutlet UILabel *outputLabel;
#end
SecondViewController.m
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
#synthesize outputLabel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
ViewController *vc = [[ViewController alloc]init];
[vc setDelegate:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)sendTextToViewController:(NSString *)stringText
{
NSLog(#"Sent text to vc");
[outputLabel setText:stringText];
}
I've looked at this and the first answer makes sense, but for some reason it's not working.
I do think that the problem is where I am setting calling [vc setDelegate:self], but not sure how to fix this. Some pointers in the right direction would be greatly appreciated. Keep in mind I'm new to obj-c so if you can explain what you are saying, that would be great. Thank you.
Your are creating a new instance of ViewController but you don't do anything with it.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
ViewController *vc = [[ViewController alloc]init];
[vc setDelegate:self];
}
The SecondViewController needs to have reference to the FirstViewController to be able to set itself as a delegate.
First you don't have to use delegation to do such a program.
A simpler way would be just creating a property in the SecondViewController that you'll pass the content of the textField into it.
Your code doesn't work because you called sendTextToViewController on a delegate that hasn't been set. You have set the delegate to a new instance of ViewController, not the one presented onscreen.

ios: delegate doesn't respond

i'm trying to create my first delegate here's what i'm trying to do
at class called PAStepper.h i did this
#class PAStepper;
#protocol StepperDelegate <NSObject>
#required
-(void)didIncrement;
-(void)didDecrement;
#end
#interface PAStepper : UIControl
{
__weak id <StepperDelegate> stepperDelegate;
}
#property (nonatomic, weak) id <StepperDelegate> stepperDelegate;
#end
and synthized the property of course in the .m file
and then putten in the PAStepper.m the following in a method
[self.stepperDelegate didDecrement];
in the controller i want to take the delegate i did this
.h
#interface OCSideCartViewController : UIViewController<StepperDelegate>
.m
- (void)viewDidLoad
{
[super viewDidLoad];
PAStepper *stepper = [[PAStepper alloc]init];
stepper.stepperDelegate = self;
}
-(void)didIncrement{
NSLog(#"inc");
}
-(void)didDecrement{
NSLog(#"dec");
}
knowing that this class is viewController that contain a table that every cell have PAStepper .. all i wanted that the delegate call didIncrement and didDecrement whenever i press the buttons
When you call this method
[self.stepperDelegate didDecrement];
NSLog you stepperDelegate and ensure it is not null. Can you confirm the method which calls your delegate is being called?
you can call this in your pastapper.h file
if ([stepperDelegate respondsToSelector:#selector(didIncrement)])
{
[stepperDelegate performSelector:#selector(didIncrement)];
}
I can´t download the project, so i am guessing..
Is the Stepper in the Interface Builder?
If yes you don´t have to allocate it.
Make an Outlet Connection of the Stepper in the OCSideCartViewController.
Post this in OCSideCartViewController:
- (void)viewWillAppear:(BOOL)animated{
self.stepper.stepperDelegate = self;
[super viewWillAppear:animated];
}
If the Stepper is not an Outlet, you have to add it to your ViewControllers view after setting it´s bounds..

How can we access values of an array added in a viewcontroller class, inside a different UIView class..?

viewcontroller.m has the following code
- (void)viewDidLoad
{
[super viewDidLoad];
self.array=[[NSArray alloc]initWithObjects:#"hi",#"hello", nil];
NSLog(#"%#",self.array);
view *view1=[[view alloc]init];
[view1 addSubview:self.view];
view1.viewController=self;
}
and there is another UIView class where I am trying to access the array :
the .h file :
#import <UIKit/UIKit.h>
#import "ViewController.h"
#class ViewController;
#interface view : UIView{
ViewController *viewController;
}
#property (nonatomic,retain)ViewController *viewController;
#end
and the .m file :
#import "view.h"
#import "ViewController.h"
#implementation view
#synthesize viewController;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSLog(#"%#",[viewController array]);
}
return self;
}
I checked in other posts of stackoverflow, and the passing of values was mentioned only between viewcontrollers; or the array was declared in the appdelegate and used in the classes(which I want to avoid).
The NSLog in the last code segment above gives null; so can you please help out in accessing the values of this array.
Thanks in advance..!!
You can achieve using this code in your ViewController
#import "view.h"
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *ary = [NSArray arrayWithObjects:#"7",#"5",#"3",#"2", nil];
view *v=[[view alloc] init];
[v initView:ary];
}
And in your view.h file :
#import <UIKit/UIKit.h>
#interface view : UIView
-(void)initView:(NSArray *)ary;
#end
And in your .m file :
#import "view.h"
#import "ViewController.h"
#implementation view
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
-(void)initView:(NSArray *)ary
{
NSLog(#"%#",ary);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
Log value will display this :
2013-02-20 20:11:52.731 SampleProject[9414:f803] (
7,
5,
3,
2
)
This line
view *view1=[[view alloc]init];
calls the desired initializer initWithFrame: before you set view1.viewController, so what's happening is that
NSLog(#"%#",[viewController array]);
actually calls
NSLog(#"%#",[null array]);
or (note that is pseudocode)
NSLog(#"%#",null);
What you'll want to do is to use view1.viewController after it is assigned. The best practice would be to make a custom constructor taking UIViewController* as a parameter and use it.
First thing you are calling the init method on the view and checking for viewController in the initWithFrame method which is never called. (But maybe you are calling the initWithFrame: from inside your init method with a default frame. :) ). Second, you are setting the viewcontroller property after you have called the init method, so your viewcontroller is still uninitialized in your initWithFrame method.
Third, instead of passing the whole of viewcontroller to your view to access the array (which kind of goes against MVC pattern), you could probably use just create an instance variable in your UIView subclass and pass just the array.
Then you could follow the answer given by Dilip, preferably using the setter method for setting the array. IMO.

How do I call the 'popoverControllerDidDismissPopover' method?

I am having trouble calling the the popoverControllerDidDismissPopover method in as much as I do not know where to put it and how to call it.
I have created a popover as follows -
// SettingsViewController.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "ViewController.h"
#import "SharedData.h"
#import "PlayerPopUpVC.h"
#interface SettingsViewController : UIViewController <UITableViewDataSource, UIPopoverControllerDelegate> {
- (IBAction)popUp:(id)sender;
#property (strong, nonatomic) UIPopoverController *playerPopUpVC;
#property (strong, nonatomic) PlayerPopUpVC *popUp;
// SettingsViewController.m
#import "SettingsViewController.h"
- (IBAction)popUp:(id)sender {
UIButton *editPlayers = (UIButton *)sender;
if(self.playerPopUpVC) {
self.popUp= [[PlayerPopUpVC alloc] initWithNibName:#"PlayerPopUpVC" bundle:nil];
self.popUp=[[UIPopoverController alloc] initWithContentViewController:self.popUp];
}
[self.playerPopUpVC presentPopoverFromRect:[editPlayers frame] inView:[editPlayers superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
I know I have to set the delegate of my PopOver to self in order to call the method but cannot work out what the code is.
I have tried -
self.playerPopUpVC.delegate=self
but Xcode does not like it.
My popOver class looks like this -
// PlayerPopUpVC.h
#import <UIKit/UIKit.h>
#interface PlayerPopUpVC : UIViewController <UITableViewDataSource, UIPopoverControllerDelegate> {
}
// PlayerPopUpVC.m
#import "PlayerPopUpVC.h"
#interface PlayerPopUpVC ()
#end
- (void)viewDidLoad
{
[super viewDidLoad];
self.modalInPopover = NO;
self.contentSizeForViewInPopover = CGSizeMake(240, 400);
}
Any help would be most welcome. I have spent a week now trying to sort it.
First, you need to understand the delegate pattern, which seems that you dont fully understand yet.
The popover will be the one which will call the popoverControllerDidDismissPopover method on the delegate. You only have to implement the UIPopoverControllerDelegate protocol in your class and assign yourself as the delegate of the popover. Why do you say that XCode doesn't like it? please, provide more info.
Furthermore, you are making an incorrect assignment here:
self.popUp=[[UIPopoverController alloc] initWithContentViewController:self.popUp];
Edit: Provided more code to help with the error. Please, review the delegate pattern next time before making these questions.
Your SettingsController.m should have this instead:
- (IBAction)popUp:(id)sender {
UIButton *editPlayers = (UIButton *)sender;
if(!self.popUp) {
self.popUp= [[PlayerPopUpVC alloc] initWithNibName:#"PlayerPopUpVC" bundle:nil];
}
self.playerPopUpVC=[[UIPopoverController alloc] initWithContentViewController:self.popUp];
self.playerPopUpVC.delegate = self;
[self.playerPopUpVC presentPopoverFromRect:[editPlayers frame] inView:[editPlayers superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
// Your code here
}

Resources