0

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

视图控制器.h

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

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

{
    NSString *testLocation;
}

@property (nonatomic, retain) NSString *testLocation;

@end

查看控制器.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

选择服务器视图.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

选择服务器视图.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];

}

出口

在这个阶段仍然没有快乐。按下导航控制器提供的后退按钮后,第一个单元格无法更新。

4

1 回答 1

1

看起来你快到了。

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

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

-(无效)viewWillAppear:(BOOL)动画{

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

}

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

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

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

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

于 2013-01-31T02:18:42.000 回答