-1

我需要对我的视图控制器有所启发。第一个视图控制器有一个UILabel和一个UIButtonUIButton通向第二个视图控制器

第二个视图控制器是一个UITableView. 此视图中的所有内容都以编程方式完成,包括UINavigationBarUINavigationItemUIBarButtonItem. 它没有UINavigationController

所以一旦用户从列表中选择了一种货币,它应该UILabel在第一个视图控制器中更新。但它不会。

第一个视图控制器.h:

#import <UIKit/UIKit.h>

#import "CurrencyListViewController.h"

@interface InformationViewController : UIViewController <CurrencyListViewControllerDelegate>

@property (nonatomic, retain) IBOutlet UILabel *currencyLabel;

- (IBAction)currencyButton;

@end

第一个视图控制器.m:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self createCurrencyButton];

    [self createAcceptDeclineButton];

    [self createCurrencyLabel];
}

- (IBAction)currencyButton
{
    CurrencyListViewController *currencyView = [[CurrencyListViewController alloc] initWithNibName:@"CurrencyListViewController" bundle:nil];
    currencyView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    currencyView.delegate = self;

    [self presentViewController:currencyView animated:YES completion:nil];
}

- (void)createCurrencyLabel
{
    currencyLabel = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 100.0, 120.0, 40.0)];

    currencyLabel.hidden = NO;
    currencyLabel.textColor = [UIColor whiteColor];
    currencyLabel.textAlignment = NSTextAlignmentCenter;

    [currencyLabel setEnabled:NO];

    [self.view addSubview:currencyLabel];
}

- (void)currencySelected: (NSString *)currencySelected
{
    currencyLabel.text = [NSString stringWithFormat:@"%@", currencySelected];

    NSLog(@"Current currency is %@", currencyLabel.text);
}

第二个视图控制器.h:

#import <UIKit/UIKit.h>

@protocol CurrencyListViewControllerDelegate <NSObject>

- (void)currencySelected: (NSString *)currencySelected;

@end

@interface CurrencyListViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
    id<CurrencyListViewControllerDelegate> delegate;
}

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, assign) id delegate;

@end

第二个视图控制器.m:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];

    UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Currency"];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle: @"Cancel"
                                                            style:UIBarButtonItemStylePlain
                                                           target:self
                                                           action:@selector(cancelButtonPressed:)];
    navigationItem.rightBarButtonItem = barItem;

    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 44.0, self.view.frame.size.width, self.view.frame.size.height - navigationBar.frame.size.height) style:UITableViewStylePlain];
    self.tableView = tableView;

    [navigationBar pushNavigationItem:navigationItem animated:NO];
    [self.view addSubview:tableView];
    [self.view addSubview:navigationBar];
    [self showCurrencies];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [arrayOfCurrencies objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    InformationViewController *informationViewController = [[InformationViewController alloc] init];

    if ([_delegate respondsToSelector:@selector(currencySelected:)]) {
        informationViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

        [_delegate currencySelected:[arrayOfCurrencies objectAtIndex:indexPath.row]];
        NSLog(@"The user selects %@", [arrayOfCurrencies objectAtIndex:indexPath.row]);
    }

    [self presentViewController:informationViewController animated:YES completion:nil];
}

我能够输出:

2013-06-05 00:16:58.731 Testing[7056:c07] Current currency is AOA: Angolan Kwanza
2013-06-04 19:08:27.222 Testing[3613:c07] 2013-06-05 00:16:58.732 Testing[7056:c07] The user selects AOA: Angolan Kwanza

但是UILabel在我的第一个视图控制器不会。

4

1 回答 1

1

为什么又出现 informationViewController 了?关闭 Current ViewController,即 CurrencyListViewController 并执行 currencySelected 功能

应该是这样的:

[self dismissModalViewControllerAnimated:YES];
[_delegate currencySelected:[arrayOfCurrencies objectAtIndex:indexPath.row]];
于 2013-06-04T17:26:24.330 回答