ProductsViewController

ProductsViewController

我有一个简单的代码,其中有两个类: ViewController (这是主要的rootViewController)和 ProductsViewController (谁是通过PopoverController打开的Controller)。

接下来发生的是,在 ViewController 类中,我有一个setter命令,负责更新UILabel:

IBOutlet UILabel *myLabel;
@property(nonatomic,retain)  NSString *saver;

- (void)viewDidLoad{
    [super viewDidLoad];
    myLabel.text = saver;
}
-(void)setLabel:(NSString*)value{
    saver = value;
    myLabel.text = value;
}

ProductsViewController 类中,我具有负责激活setter方法的代码:
ViewController *detailViewController = [[ViewController alloc] init];
[detailViewController setLabel:@"Changes?"];
[detailViewController release];

我认为问题在于打开弹出窗口的时间,因为我认为UILabel处于不 Activity 状态,因为如果将NSLog(@"Exists -> %@",myLabel);放入setter方法中,则会得到(空)

有人可以帮我解决这个问题,以及如何解决这个问题吗?

谢谢。

编辑

我试图以这种方式使用协议:

ViewController.h
#import <UIKit/UIKit.h>
#import "ProductsViewController.h"

@interface ViewController : UIViewController <ProductsViewControllerDelegate>{

    IBOutlet UILabel *myLabel;
}

-(IBAction)showPopOver:(id)sender;
- (void)productWasSelected:(NSString *)product;
@end

ViewController.m
- (void)presentProductsViewController:(ProductsViewController *)productsViewController {
    productsViewController.delegate = self;
    //present in popover here...
}

- (void)productWasSelected:(NSString *)product{
    myLabel.text = product;
}

ProductsViewController.h
#import <UIKit/UIKit.h>

@protocol ProductsViewControllerDelegate <NSObject>
- (void)productWasSelected:(NSString *)product;
@end

@interface ProductsViewController : UIViewController
@property (nonatomic, retain) id<ProductsViewControllerDelegate> delegate;
@end

ProductsViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate productWasSelected:@"Selected Product"];
    NSLog(@"Called!");
}

我的ShowPopover方法如下:
-(IBAction)showPopOver:(id)sender{

    UIButton *btn =sender;
    ProductsViewController *productsView =[[ProductsViewController alloc] initWithNibName:@"ProductsViewController" bundle:nil];
    popOver =[[UIPopoverController alloc] initWithContentViewController:productsView];
    [popOver presentPopoverFromRect:btn.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

    [productsView release];

}

最佳答案

共享同一类的两个对象不要使它们成为同一对象。您可能要在这里使用委托。在线上有很多教程。这是一个例子

@protocol ProductsViewControllerDelegate <NSObject>
- (void)productWasSelected:(NSString *)product;
@end

@interface ProductsViewController : UIViewController
@property (nonatomic, weak) id<ProductsViewControllerDelegate> delegate;
@end


@implementation ProductsViewController

- (void)didSelectProduct {
    [self.delegate productWasSelected:@"Selected Product"];
}

@end
@interface MainViewController : UIViewController <ProductsViewControllerDelegate>
@end


@implementation MainViewController

- (IBAction)showPopOver:(id)sender{
    UIButton *btn =sender;
    ProductsViewController *productsView =[[ProductsViewController alloc] initWithNibName:@"ProductsViewController" bundle:nil];
    productsView.delegate = self;
    popOver =[[UIPopoverController alloc] initWithContentViewController:productsView];
    [popOver presentPopoverFromRect:btn.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}

- (void)productWasSelected:(NSString *)product {
    self.myLabel.text = product;
}

@end

关于ios - 如何通过UIPopoverController更改UILabel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23814768/

10-13 03:51