点击后获取代表的价值?

时间:2013-01-31 02:05:20

标签: uiviewcontroller ios6 delegates uinavigationcontroller protocols

我有两个类文件。 ViewController和ChooseServerView。用户单击ViewController类中的一个单元格,该单元格将推送到ChooseServerView。当用户在ChooseServerView中进行选择时,该单元格的值将传递给该委托。现在,这两个视图位于导航控制器的前面,因此在ChooseServerView中有一个后退按钮。当用户点击回来时我想用第一个视图中的新值更新代理中的单元格,有意义吗?

ViewController.h

#import <UIKit/UIKit.h>
#import "ChooseServerView.h"

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, serverUserChoice>

{
    NSString *testLocation;
}

@property (nonatomic, retain) NSString *testLocation;

@end

查看Controller.m

#import "ViewController.h"
#import "ChooseServerView.h"
#import "AppDelegate.h"


@interface ViewController ()

@end

@implementation ViewController
@synthesize testLocation;



- (void)viewDidLoad
{


   NSUserDefaults *sharedPref = [NSUserDefaults standardUserDefaults];
   testLocation =[sharedPref stringForKey:@"defaultLocation"];

    NSLog(@"Location Chosen: %@",testLocation);


    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}



#pragma mark - Table View Methods


- (void) userDidChoose:(NSString *) server {

    testLocation = server;
    NSLog(@"Test Location %@", server);

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;              // Default is 1 if not implemented
{
    return 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    // fixed font style. use custom view (UILabel) if you want something different

{

    switch (section) {
        case 0:
            return @"Choose Test Location:";
            break;
        case 1:
             return @"Choose Test Type:";
        default:
            return @"Unknown";
            break;
    }

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

{
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 1;
        default:
            return 0;
            break;
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

{


    UITableViewCell *serverLocCell = [tableView dequeueReusableCellWithIdentifier:@"serverLocation"];



    switch (indexPath.section) {
        case 0:
            serverLocCell.textLabel.text = testLocation;
            serverLocCell.detailTextLabel.text = @"Change";

            break;
        case 1:
            serverLocCell.textLabel.text = @"Speed Test";
            serverLocCell.detailTextLabel.text = @"Change";
            break;
        default:
            break;
    }

    return serverLocCell;

}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    switch (indexPath.section) {
        case 0:
       [self performSegueWithIdentifier:@"toServerChoice" sender:self];
            break;
        case 1:
           [self performSegueWithIdentifier:@"toTestType" sender:self];
            break;
        default:
            break;
    }

}
@end

ChooseServerView.h

#import <UIKit/UIKit.h>

@class ViewController;

@protocol serverUserChoice <NSObject>
@optional

- (void)userDidChoose:(NSString *) server;

@end



@interface ChooseServerView : UIViewController <UITableViewDataSource, UITableViewDelegate, serverUserChoice>

{
    NSArray *serverSelection;
    NSArray *tqServerSelection;
}

@property (nonatomic, retain) NSArray *serverSelection;
@property (nonatomic, retain) NSArray *qServerSelection;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(retain) NSIndexPath* lastIndexPath;
@property (nonatomic, assign) id <serverUserChoice> serverDelegate;

@end

ChooseServerView.m

#import "ChooseServerView.h"
#import "ViewController.h"
#import "AppDelegate.h"

#define totalSections 2
#define standardSection 0
#define qualitySection 1

@interface ChooseServerView ()

@end

@implementation ChooseServerView;
@synthesize serverSelection;
@synthesize qServerSelection;
@synthesize lastIndexPath;
@synthesize serverDelegate;

- (void)viewDidLoad
{

    serverSelection = [[NSArray alloc] initWithObjects:@"Chicgo, IL",@"London, UK",@"San Jose, CA",@"Washington, DC", nil];
    qServerSelection = [[NSArray alloc] initWithObjects:@"Chicgo, IL (Q)",@"London, UK (Q)",@"San Jose, CA (Q)",@"Washington, DC (Q)", nil];
    [super viewDidLoad];

}


#pragma mark - Table View Methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;              // Default is 1 if not implemented
{
    return totalSections;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    // fixed font style. use custom view (UILabel) if you want something different

{
    switch (section) {
        case standardSection:
            NSLog(@"Std Heading Set");
            return @"Standard Test Locations:";
            break;
        case qualitySection:
            NSLog(@"Qual Heading Set");
            return @"Quality Test Locations:";
            break;
        default:
            NSLog(@"Section Count Error");
           return @"Section Count Error";
            break;
    }


}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

{
    if (section == 0) {
        NSLog(@"Std Loc Set");
        return [serverSelection count];

    }
    else {
        NSLog(@"Quality Loc Set");
        return [qServerSelection count];

    }
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

{


    UITableViewCell *serverLoc = [tableView dequeueReusableCellWithIdentifier:@"serverSelection"];



    switch (indexPath.section) {
        case standardSection:
            serverLoc.textLabel.text = [self.serverSelection objectAtIndex:indexPath.row];

            break;
        case qualitySection:
            serverLoc.textLabel.text = [self.qServerSelection objectAtIndex:indexPath.row];
            break;
        default:
            break;
    }


    return serverLoc;

}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


    NSString *cellvalue;

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cellvalue = cell.textLabel.text;
    [serverDelegate userDidChoose:cellvalue];
    NSLog(@"Cell Selected is %@",cellvalue);
    [cell setSelected:FALSE animated:TRUE];


}
@end

我可以看到正确的值传递给委托,我只是不知道如何使用导航控制器实现的自动后退按钮“调用”该委托/方法。

任何想法都会很棒。

根据以下答案:

添加到我的ViewController.h

@property (nonatomic, weak) IBOutlet UITableView *tableView;

添加到我的ViewController.m

-(void) viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    [self.tableView reloadData];

}

Outlet

现阶段仍然没有喜悦。按导航控制器提供的后退按钮后,第一个单元无法更新。

1 个答案:

答案 0 :(得分:1)

看起来你快到了。

我认为问题是您的UITableView没有被重新加载以表示testLocation的更新值。我没有看到由ViewController管理的UITableView的引用,首先向ViewController.m添加一个插座以指向此表视图。

然后,在ViewController.m中添加以下内容

- (void)viewWillAppear:(BOOL)动画{

[super viewWillAppear:animated];
[self.tableView reloadData];

}

您还需要确保将ViewController设置为ChooseServerViewController的委托。 (为了清楚起见,你应该将ChooseServerView.m重命名为ChooseServerViewController.m,因为它是一个视图控制器子类 - 我在下面提到过它)

您可以通过在ViewController.m中添加一个准备segue方法来设置委托 - 类似于:

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { ChooseServerViewController * viewController =(ChooseServerViewController *)segue.destinationViewController;     viewController.serverDelegate = self; }

我还会考虑将UITableViewController用于这两个控制器类,因为它似乎是您正在寻找的功能类型。