ビュー コントローラーに関する知識が必要です。最初のView ControllerにはUILabel
と がありUIButton
ます。は、 2 番目の View ControllerUIButton
につながります。
2 番目のビュー コントローラーはUITableView
. このビューのすべては、UINavigationBar
、UINavigationItem
、およびUIBarButtonItem
. いいえUINavigationController
。
したがって、ユーザーがリストから通貨を選択するUILabel
と、最初の View Controllerで が更新されます。しかし、そうはなりません。
最初のView Controller .h:
#import <UIKit/UIKit.h>
#import "CurrencyListViewController.h"
@interface InformationViewController : UIViewController <CurrencyListViewControllerDelegate>
@property (nonatomic, retain) IBOutlet UILabel *currencyLabel;
- (IBAction)currencyButton;
@end
最初のView Controller .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);
}
2 番目のビュー コントローラー .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
2 番目のビュー コントローラー .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
私の最初のView Controllerではそうではありません。