0

I have a view controller which contains a tableview.This tableview is displayed in the UIPopover controller in a parent view.I want the text from the selected cell in the popover controller to get set in a UITextField in the parent view and i want to dismiss the popover after selection.I am not able to achieve this.

Code of the popover controller

.h file

#import <UIKit/UIKit.h>

@protocol SelectLocationViewControllerDelegate <NSObject>

- (void)locationSelected:(NSString *)location;

@end

@interface SelectLocationViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
    IBOutlet UITableView *locationTableView;
    NSArray *locationtypes;
    id delegate;


}

@property (nonatomic, retain) UITableView * locationTableView;
@property (nonatomic, retain) NSArray * locationtypes;
@property (nonatomic, assign) id<SelectLocationViewControllerDelegate> delegate;


@end

.m file of the popover

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSInteger row = [indexPath row];

    NSString *locationSelected = [self.dwellingTypes objectAtIndex:row];

    [self.delegate locationSelected: locationSelected];  // This don't gets invoked.

}

Parent Class

- (void) locationSelected:(NSString *)location {

    ----Here i set the the text for text field and dismiss the popover----
    [popoverController dismissPopoverAnimated:YES];
}

The locationselected method which is present in the parent class doesn't gets called.

Please any body help me to over come from this issue.

Thank You

Is the popover i am creating is correct?

.h file

#import <UIKit/UIKit.h>
#import "SelectLocationViewController.h"
@interface SearchViewController : UIViewController<SelectLocationViewControllerDelegate,UIPopoverControllerDelegate>{

    SelectLocationViewController * selectLocationViewController;
    UIPopoverController * locationpopover;
    IBOutlet UITextField *locationSelectedField;

}
@property (nonatomic, retain) UIPopoverController * locationpopover;
@property (nonatomic, retain) SelectLocationViewController * selectLocationViewController;



.m file

- (void)viewDidLoad {

selectLocationViewController=[[SelectLocationViewController alloc]init];  //The class which i am displaying inside the popover
selectLocationViewController.delegate=self;
UINavigationController *navigationcontroller=[[UINavigationController alloc]initWithRootViewController: selectLocationViewController];

locationpopover = [[UIPopoverController alloc] initWithContentViewController:navigationcontroller]; 
[locationpopover setPopoverContentSize:CGSizeMake(290,410) animated:YES];
[locationpopover setDelegate:self];

}

- (void)itemSelected:(NSString *)dwelling //This is the method which is called from the other class when a row is selected from the tableview in SelectLocationViewController class
{    

    locationSelectedField.text= dwelling;
    NSLog(@"DwellingSelectedField iside tap:%@",dwelling);   //I get the text printed here
    [locationpopover dismissPopoverAnimated:YES];

}
Sankar Chandra Bose
  • 377
  • 1
  • 12
  • 27

2 Answers2

3

I believe it is not called because you haven't set your delegate property. You should check this part of code. Or if it is ok add it to your post.

Zapko
  • 2,461
  • 25
  • 30
  • Usually you set delegates when you create on object that needs a delegate. So you should insert it to the place where you create SelectLocationViewController. – Zapko Jun 02 '11 at 06:12
  • Zapko...i got the method invoked by using [popviewcontrollerobj setDelegate:object_of_the_class_where_mthd_is_present];Thanks for your help... – Sankar Chandra Bose Jun 02 '11 at 07:23
  • Now my problem is popover doesn't gets dismissed and the selected cells value doesn't comes to the UITextField,but i have a NSLog inside that method,there it gets printed......I am looking into that issue...If you have any ideas also please let me know – Sankar Chandra Bose Jun 02 '11 at 07:31
  • ) I suppose that your popoverController variable is not pointing to your popover controller, now. – Zapko Jun 02 '11 at 09:21
  • Hi ZapKo i have one query.Should the view which is displayed inside the popover controller be always a sub class of a UITableViewController or it can be the subclass of a UIViewController class.Why i am asking this is because the popover controller which i am currently displaying is a subclass of UIViewController class...Kindly advise... – Sankar Chandra Bose Jun 02 '11 at 10:23
  • @Sankar, surely it can be any kind of UIViewController or its subclasses. You can check [documentation](http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIPopoverController_class/Reference/Reference.html%23//apple_ref/doc/uid/TP40009306) documentation on this matter – Zapko Jun 02 '11 at 10:41
  • Zapko i went through the documentation,Thanks for the link.I have edited my post and have added the code to show you how i create popover and how i dismiss.Can you please say me am i creating the popover and dismissing it correctly – Sankar Chandra Bose Jun 02 '11 at 11:05
  • @Sankar, it looks ok. Does it work as expected? PS You can release selectLocationViewController as soon as you gave it to popover. – Zapko Jun 02 '11 at 11:34
  • I have released it.I am really fed up with this issue.Still i am not able to find the solution.Thanks for replying all these time.Sorry for disturbing you often.I have looked all over the internet for so many hours.i dont find a solution.I try my best to resolve the issue. – Sankar Chandra Bose Jun 02 '11 at 12:05
  • Zapko is solved the issue.You are a great genius..What you have suspected in your first reply is true.The problem was with my delegate.Please check my answer for the same question and kindly let me know your feedbacks – Sankar Chandra Bose Jun 06 '11 at 04:35
0

The problem is with delegate.I changed the method invocation from

[self.delegate locationSelected: locationSelected]

where location locationSelected is a NSString which holds the string from a selected cell.

to

[delegate locationSelected: locationSelected]; 

for example if i have created a protocol like

@protocol locationControllerDelegate <NSObject>

- (void)locationSelected:(NSString *)location;

@end

and in the interface of the class where protocol is declared,it should be in the following manner

 @interface SelectLocationViewController : UIViewController <UITableViewDataSource, UITableViewDelegate,locationControllerDelegate> {

.
.
 id delegate;


}

@property (nonatomic, assign) id<locationControllerDelegate> delegate;


@end

and in the didSelectForRowAtIndexPath method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSInteger row = [indexPath row];

    NSString *locationSelected = [locationTypes objectAtIndex:row];

    [delegate locationSelected: locationSelected]; 

}

and in the class where the method is implemented in the .h file in the interface the protocol should be inherited as like we use other delegates(UISCrollViewDelegate etc.,)and in the .m file its like a normal method implementation we can implement the method defined in the protocol

So whenever a row is selected in the TableView,this method will be invoked and the string will be set to the label or the textfield which ever you wish to set the text

Sankar Chandra Bose
  • 377
  • 1
  • 12
  • 27
  • I'm glad you've solved your problem and sorted out a bit of Objective-C. I recommend you to read apples [documentation about objecteve-c](http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html%23//apple_ref/doc/uid/TP30001163). And good luck with your project! – Zapko Jun 06 '11 at 05:56
  • Thanks Zapko....I will read the documentation...and make use of it..Thanks for your feedback... – Sankar Chandra Bose Jun 06 '11 at 06:52
  • HI Zapko i have another problem i have posted my question in this link can you say what i have to do http://stackoverflow.com/questions/6254571/uitableview-in-the-detailed-view-of-the-uisplitviewcontroller-ipad....Thanks – Sankar Chandra Bose Jun 07 '11 at 04:52