0

我试图在弹出窗口中单击按钮时关闭弹出窗口并返回变量。有四个按钮,在选择一个按钮时,变量(基于按钮)将返回到原始视图控制器并且弹出框将关闭。我不确定如何尝试传递变量,但我正在尝试使用此页面(http://stackoverflow.com/questions/3565968/dismiss-popover-using-uibutton)至少在按钮单击时关闭弹出框),由于某种原因,这对我不起作用。当我单击按钮时,绝对没有任何反应。

AddWineViewController 是“根”视图控制器

//AddWineViewController.h
//this is the "root" view controller
#import "WineStyleViewController.h"
@interface AddWineViewController : UIViewController <UIPopoverControllerDelegate, MyPopoverDelegate>
@property (nonatomic, retain) UIPopoverController *myPopoverController;

 

//AddWineViewController.m
@implementation AddWineViewController
@synthesize myPopoverController;
-(void)didClickCancelButton {
    //I would like to have the variable passed here, something like self.wineStyle.text=wineStyle; where wineStyle is the variable from the popover.
    [myPopoverController dismissPopoverAnimated:YES];
}

WineStyleViewController 是弹出视图控制器

//WineStyleViewController.h
@protocol MyPopoverDelegate <NSObject>
    -(void)didClickCancelButton;
@end
@interface WineStyleViewController : UIViewController
@property (nonatomic, assign) id<MyPopoverDelegate> delegate; 
@property (nonatomic, strong) NSString *wineStyle;
- (IBAction)redWineButton:(id)sender;

 

//WineStyleViewController.m
@implementation WineStyleViewController
@synthesize wineStyle;
@synthesize delegate;

- (IBAction)redWineButton:(id)sender {
    wineStyle=@"Red";
    [self.delegate didClickCancelButton];
}
4

5 回答 5

1

我将首先做一些假设,即故事板中的所有连接都已连接,并且所有对象都已正确创建,并且您已在 segue 中将委托设置为 self。

只是为了确定......确保你的segue包括:

// set your destination view controller
WineStyleViewController *destinationController = segue.destinationViewController;
destinationController.delegate=self;

在您的委托(弹出窗口):WineStyleViewController 中,您将协议定义为:

@protocol MyPopoverDelegate <NSObject>
    -(void)didClickCancelButton;
@end

但是您想将 wineStyle 传回根控制器 (AddWineViewController),因此您需要 1) 添加变量 (wineStyle) 以传回和 2) 传递您的委托的 ViewController(我冒昧地更改为保存而不是取消)

@protocol MyPopoverDelegate <NSObject>
-(void)didClickSaveButton (WineStyleViewController *)wineStyleViewController withWineStyle (NSString *)wineStyle;
@end

现在回到 AddWineViewController:

your-(void)didClickCancelButton:(WineStyle)wineStyle是你从你的代表那里收到你的葡萄酒风格的地方,所以这个方法应该更像:(再次改变它保存)

-(void)didSave
{
    // your methods for dealing with your added wineStyle go hear, as
    // you will already have wineStyle sent back to you from your delegate
    [myPopoverController dismissPopoverAnimated:YES];
} 

希望有帮助:D

于 2012-08-05T11:08:29.377 回答
1

要将“wineStyle”传递给您的“MyPopover”协议,只需将变量添加到协议中:

@protocol MyPopoverDelegate <NSObject>
    -(void)didClickCancelButton:(WineStyle)wineStyle;
@end

..并确保将其发送给代表,如下所示:

if ([delegate conformsToProtocol:@protocol(MyPopoverDelegate)]) {
   if ([delegate respondsToSelector:@selector(didClickCancelButton:)]) {
       [delegate didClickCancelButton:selectedWineStyle];
   }
}

关于 Popover 未被解除,请确保存储对 UIPopoverController 的引用并简单地调用:

- (void)didClickCancelButton:(WineStyle)wineStyle {
   // ... do something with "wineStyle" here
   [currentPopover dismissPopoverAnimated:YES];
}
于 2012-07-17T14:04:13.157 回答
1

我假设您已正确建立 IBAction 连接(并且当您按下按钮时该方法实际上正在执行);所以我的下一个想法是,self.delegate当控制器出现在屏幕上时,没有正确设置(在显示AddWineViewController弹出框的方法中)。

另外,我应该提一下,我可能会稍微改变一下协议的接口,但要记住一些事情:

  1. 我不会告诉代表按下取消来触发弹出框的解除;告诉它已经进行了选择。
  2. 我更喜欢在签名中传递“孩子”的协议方法:[self.delegate didChooseWineStyle: self]
于 2012-07-17T13:58:30.970 回答
0

This tutorial really helped me out with a similar issue: http://ios.biomsoft.com/2011/10/17/beginning-storyboards-in-ios-5-part-2/

于 2012-07-20T15:25:16.120 回答
0

您需要告诉弹出框控制器要拥有什么弹出框,您没有这样做。我不确定你是如何表达你的观点的,但你应该有类似的东西

   UIPopoverController paparazzi = [[UIPopoverController alloc] initWithContentViewController:temp];
于 2012-07-17T13:56:22.957 回答