Мне нужно просветление с помощью контроллеров просмотра. Контроллер первого вида имеет UILabel
и UIButton
. UIButton
приводит к контроллеру второго взгляда .UILabel не будет обновлять
контроллер второго взгляда - UITableView
. Все в этом представлении делается программно, включая UINavigationBar
, UINavigationItem
и UIBarButtonItem
. Он имеет NO 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
в моем первого контроллера представления будут НЕ.
Где ваш «первый контроллер представление»? Где вы его создаете? Где указатель на него? –
Я вижу то, что имеет то же имя, что и «контроллер первого вида», но создается после всего остального, и метод 'currencySelected' никогда не вызывается. Вместо этого вызывается метод * different * _delegate. –
На самом деле, я не вижу нигде, где установлен _delegate, поэтому, вероятно, вы делаете вызов указателем на нуль. –