我是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
答案 0 :(得分:0)
您的第一个NSLog语句是否会被解雇? “OK按钮按下”。如果没有,请确保(IBAction)okButtonPressed已在Interface Builder中连接。如果是这样,但是[delegate scoreAssigned:@“100”]的断点;并用鼠标将鼠标悬停在“delegate”上以查看它是否为零,只是为了查看委托是否已成功分配。如果这不能解决问题,请在事情停止工作之前告诉我们您到达的确切位置。
正确呈现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。