我有一个包含tableview.This表格视图的视图控制器,它显示在UIPopover控制器的父视图中。我想要在父视图的UITextField中设置popover控制器中选定单元格的文本,并且希望在选择后关闭popover。我无法实现这一点。
popover控制器的代码
.h文件
#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文件
- (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.
}父类
- (void) locationSelected:(NSString *)location {
----Here i set the the text for text field and dismiss the popover----
[popoverController dismissPopoverAnimated:YES];
}父类中存在的locationselected方法不会被调用。
请任何人帮我解决这个问题。
谢谢
我创建的popover是否正确?
.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];
}发布于 2011-06-01 20:41:59
我相信它不会被调用,因为你没有设置你的委托属性。您应该检查这部分代码。或者,如果可以,请将其添加到您的帖子中。
发布于 2011-06-06 12:26:43
问题出在委托上。我将方法调用从
[self.delegate locationSelected: locationSelected]
其中,location locationSelected是保存所选单元格中的字符串的NSString。
至
[delegate locationSelected: locationSelected]; 例如,如果我创建了一个协议,比如
@protocol locationControllerDelegate <NSObject>
- (void)locationSelected:(NSString *)location;
@end并且在声明协议的类的接口中,它应该采用以下方式
@interface SelectLocationViewController : UIViewController <UITableViewDataSource, UITableViewDelegate,locationControllerDelegate> {
.
.
id delegate;
}
@property (nonatomic, assign) id<locationControllerDelegate> delegate;
@end在didSelectForRowAtIndexPath方法中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = [indexPath row];
NSString *locationSelected = [locationTypes objectAtIndex:row];
[delegate locationSelected: locationSelected];
}在接口的.h文件中实现方法的类中,应该像使用其他委托(UISCrollViewDelegate等)一样继承协议,而在.m文件中,就像实现正常的方法实现一样,我们可以实现协议中定义的方法
因此,只要在TableView中选择了一行,就会调用此方法,并将字符串设置为您希望设置文本的标签或文本字段
https://stackoverflow.com/questions/6200898
复制相似问题