0

我有 1 个 tabbarcontroller 有登录和完成选项卡。我想点击 LoginTab 中的 Logout 按钮,完成选项卡中的所有数据和 tableview 都被重置。我使用了这段代码,但它不起作用:

在 CompleteTab.m 中:

-(void) viewDidLoad
{
    temp = [[NSMutableArray alloc] init];
    iduploadArray= [[NSMutableArray alloc] init];
    FileCompletedArray = [[NSMutableArray alloc] init];
    DiffArr= [[NSMutableArray alloc] init];

    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    tableData = [[NSArray alloc] initWithObjects:FileCompletedArray,nil];
    // [_tableView setAllowsSelection:YES];
    //tableView.dataSource = self;
    // [self.view addSubview:tableView];

    // [self parseXMLFile:@"x"];

    NSURL *url1 = [NSURL URLWithString:@"server.com"];

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL: url1] ;

        ....
        temp = [FileCompletedArray mutableCopy];

        _tableView.dataSource = self;
        _tableView.delegate=self;

        [self.view addSubview:_tableView];

       }
                                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                          NSLog(@"error: %@", error);//: %@",  operation.responseString);

                                      }
     ];
    [httpClient enqueueHTTPRequestOperation:operation];

}

-(void) resetAll
{
    [temp removeAllObjects];
    [FileCompletedArray removeAllObjects];
    [DiffArr removeAllObjects];
    [_tableView reloadData];
}

在 LoginTab.m 中:

- (IBAction)LogoutButton:(id)sender {

    login = false;
    username.hidden = NO;
    pass.hidden = NO;

    LoginButton.hidden = NO;
    LogoutButton.hidden = YES;

    //make all the arrays get reset
    username.text =@"";
    pass.text = @"";
    [completeview resetAll];

}

我调试:当我单击注销按钮时,它调用了 resetAll() 函数,但每个变量都是 0x000000。所以它不能重置数据和重新加载tableview。你能帮助我吗?谢谢

4

5 回答 5

1

由于注销按钮位于 loginTab 中,因此它对您的 completeTab 没有任何引用。因此,您需要将 completeTab 引用保留在应用程序的某个位置。最好的选择是在 appDelegate 中。然后在注销按钮上单击,获取此实例并重置所有数据。

像这样在 AppDelegate 中保留您的 completab 视图控制器实例

@property (nonatomic, retain) UIViewController *completeTab;

self.completeTab = \\Your complete tab viewcontroller instance.    

然后在注销按钮上单击

[(AppDelegate *)[[UIApplication sharedApplication] delegate].completTab resetAll]

希望这可以帮助。

于 2013-07-15T10:52:28.563 回答
0

我还建议您查看免费的 Sensible TableView 框架,因为它具有开箱即用的休息功能。

于 2013-07-15T19:24:36.050 回答
0

像这样使用选择器调用重置方法

[completeview performSelector:@selector(resetAll)];
于 2013-07-15T09:48:29.817 回答
0

请使用基于选项卡的应用程序创建示例项目。

====== AppDelegate.m ======

#import "LoginTab.h"

#import "CompleteTab.h"

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    UIViewController *viewController1 = [[[LoginTab alloc] initWithNibName:@"LoginTab" bundle:nil] autorelease];
    UIViewController *viewController2 = [[[CompleteTab alloc] initWithNibName:@"CompleteTab" bundle:nil] autorelease];
    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = @[viewController1, viewController2];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

@end

====== LoginTab.m ======

@implementation LoginTab

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"LoginTab", @"LoginTab");
        self.tabBarItem.image = [UIImage imageNamed:@"LoginTab"];
    }
    return self;
}

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

- (IBAction)LogoutButton:(id)sender {

    // Post Notification to CompleteTab Class

    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"resetAll"
     object:self];

}

====== CompleteTab.m ======

@implementation CompleteTab

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"CompleteTab", @"CompleteTab");
        self.tabBarItem.image = [UIImage imageNamed:@"CompleteTab"];
    }


    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(resetAll:)
                                                 name:@"resetAll"
                                               object:nil];

    return self;
}

- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (void) resetAll:(NSNotification *) notification
{
    NSLog(@"Reset All Called");
}

我可以使用 NSNotification 概念调用 CompeteTab 的 resetAll 方法。请遵循此代码。

于 2013-07-15T09:50:17.187 回答
0

至于方法 [tableView reloadData] :当表视图的委托或数据源希望表视图完全重新加载其数据时,会调用此方法。不应在插入或删除行的方法中调用它,尤其是在通过调用 beginUpdates 和 endUpdates 实现的块中。我相信您在 resetAll 中调用了 reloadData,它正在更新为表提供数据源的 FileCompletedArray。你能从别的地方打电话吗?

于 2013-07-15T22:25:40.237 回答