UIPopover问题

发布于 2024-11-11 15:08:17 字数 2902 浏览 7 评论 0原文

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

弹出窗口控制器的代码

.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

- (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];
}

.m 文件父类中存在的 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];

}

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];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的旅程 2024-11-18 15:08:17

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

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.

半窗疏影 2024-11-18 15:08:17

问题在于 delegate。我更改了

[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 中选择一行时,这将调用方法并将字符串设置为您希望设置文本的标签或文本字段

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文