gpt4 book ai didi

IOS5 iPad 弹出 Controller 委托(delegate)不向父 Controller 发送消息

转载 作者:可可西里 更新时间:2023-11-01 06:16:45 25 4
gpt4 key购买 nike

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

在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(弹出 View Controller )

#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

最佳答案

您的第一个 NSLog 语句是否触发? “按下确定按钮”。如果没有,请确保 (IBAction)okButtonPressed 在 Interface Builder 中连接。如果是这样,但是 [delegate scoreAssigned:@"100"] 上的断点;并将鼠标悬停在“委托(delegate)”上,查看它是否为零,只是为了查看委托(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。

关于IOS5 iPad 弹出 Controller 委托(delegate)不向父 Controller 发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10451679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com