首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIPopover问题

UIPopover问题
EN

Stack Overflow用户
提问于 2011-06-01 20:10:10
回答 2查看 849关注 0票数 0

我有一个包含tableview.This表格视图的视图控制器,它显示在UIPopover控制器的父视图中。我想要在父视图的UITextField中设置popover控制器中选定单元格的文本,并且希望在选择后关闭popover。我无法实现这一点。

popover控制器的代码

.h文件

代码语言:javascript
复制
#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文件

代码语言:javascript
复制
- (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.

}

父类

代码语言:javascript
复制
- (void) locationSelected:(NSString *)location {

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

父类中存在的locationselected方法不会被调用。

请任何人帮我解决这个问题。

谢谢

我创建的popover是否正确?

代码语言:javascript
复制
.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];

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-06-01 20:41:59

我相信它不会被调用,因为你没有设置你的委托属性。您应该检查这部分代码。或者,如果可以,请将其添加到您的帖子中。

票数 3
EN

Stack Overflow用户

发布于 2011-06-06 12:26:43

问题出在委托上。我将方法调用从

[self.delegate locationSelected: locationSelected]

其中,location locationSelected是保存所选单元格中的字符串的NSString。

代码语言:javascript
复制
[delegate locationSelected: locationSelected]; 

例如,如果我创建了一个协议,比如

代码语言:javascript
复制
@protocol locationControllerDelegate <NSObject>

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

@end

并且在声明协议的类的接口中,它应该采用以下方式

代码语言:javascript
复制
 @interface SelectLocationViewController : UIViewController <UITableViewDataSource, UITableViewDelegate,locationControllerDelegate> {

.
.
 id delegate;


}

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


@end

在didSelectForRowAtIndexPath方法中

代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSInteger row = [indexPath row];

    NSString *locationSelected = [locationTypes objectAtIndex:row];

    [delegate locationSelected: locationSelected]; 

}

在接口的.h文件中实现方法的类中,应该像使用其他委托(UISCrollViewDelegate等)一样继承协议,而在.m文件中,就像实现正常的方法实现一样,我们可以实现协议中定义的方法

因此,只要在TableView中选择了一行,就会调用此方法,并将字符串设置为您希望设置文本的标签或文本字段

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6200898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档