4

我是 iOS 编程的新手,我无法让这个简单的概念起作用——我只希望弹出控制器能够调用方法并在父控制器上发送数据。谁能发现我在这里做错了什么?

在 DetailViewController.h

#import <UIKit/UIKit.h>
#import "Employee.h"
#import "CompleteViewController.h"

@class EmployeesTVC;


@interface DetailViewController : UIViewController <UISplitViewControllerDelegate, UIPopoverControllerDelegate>

@property (strong) UIPopoverController *popController;

-(IBAction)completeButtonPressed:(id)sender;
@end

在 DetailViewController.m 中(取出不相关的部分)

@implementation DetailViewController {
    __weak UIPopoverController *completePopover;
}

// ...

#pragma mark - Complete / Score popover methods

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSLog(@"preparing for segue");
    UIStoryboardPopoverSegue *popoverSegue = (UIStoryboardPopoverSegue *)segue;
    UIPopoverController *scorePopoverController = [popoverSegue popoverController];
    [scorePopoverController setDelegate:self];
    self.popController = scorePopoverController;
}

- (IBAction)completeButtonPressed:(id)sender {
    if (completePopover) {
        [completePopover dismissPopoverAnimated:YES];
    } else {
        [self performSegueWithIdentifier:@"showScorePopover" sender:sender];    
    }
}

- (void) scoreAssigned:(NSString *)score {
    NSLog(score);
    NSLog(@"Score Assigned");
}

// ...

@end

CompleteViewController.h(弹出视图控制器)

#import <UIKit/UIKit.h>

@protocol CompleteViewDelegate <NSObject>
- (void)scoreAssigned:(NSString *)score;
@end


@interface CompleteViewController : UIViewController

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

- (IBAction)okButtonPressed:(id)sender;

@end

CompleteViewController.m

#import "CompleteViewController.h"

@implementation CompleteViewController
@synthesize delegate;


- (IBAction)okButtonPressed:(id)sender {
    NSLog(@"OK Button Pressed");
    [delegate scoreAssigned:@"100"];
}

@end
4

1 回答 1

0

你的第一个 NSLog 语句触发了吗?“按下确定按钮”。如果没有,请确保 (IBAction)okButtonPressed 已在 Interface Builder 中连接。如果是这样,但是在 [delegate scoreAssigned:@"100"] 上有一个断点;并用鼠标将鼠标悬停在“代表”上以查看它是否为零,只是为了查看代表是否已成功分配。如果这不能解决问题,请在事情停止工作之前让我们确切知道您要去哪里。

正确呈现 UIPopoverController

[completePopover setDelegate:self];
[completePopover presentPopoverFromBarButtonItem:yourButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
//or if you don't have a bar button item
[completePopover presentPopoverFromRect:CGRectMake(0.0, 0.0, 0.0, 0.0) inView:yourTargetView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

请记住,inView 可以是 UIView 的任何子类,例如 UIButton。

于 2012-05-04T15:29:43.007 回答