我有一个包含tableview的视图控制器。这个表视图显示在父视图中的UIPopover控制器中。我希望弹出控制器中选定单元格的文本在父视图中的UITextField中设置我想在选择后解雇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方法。
请任何人帮我解决这个问题。
谢谢
我创建的弹出窗口是否正确?
.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];
}
答案 0 :(得分:3)
我认为它没有被调用,因为你还没有设置你的委托属性。你应该检查这部分代码。或者,如果可以,请将其添加到您的帖子中。
答案 1 :(得分:0)
问题在于委托。我从
更改了方法调用 [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中选择了一行,就会调用此方法,并将字符串设置为您希望设置文本的标签或文本字段