0
votes

I have a UITableView inside of an UITabBarController. Now, I am already checking to see if a user is logged in, and if they are not logged in I display a Modal View Controller to have them log in or sign up. But here's my problem, when the user hit's cancel, the user goes back to the original position of the table view. I can't have users see the rows in table view if they are not signed in. If a user is not logged in, I want a message exactly how the examples Apple gives in the iTunes store and the "Popular near me" examples in the Apple Human Interface Guideline:

https://developer.apple.com/library/iOS/documentation/userexperience/conceptual/mobilehig/StartingStopping.html#//apple_ref/doc/uid/TP40006556-CH52-SW1

So how would I display a message like that in a tableview controller? Keep in mind, I will always have data for that table view controller. Hopefully I am clear for everyone. Would I just bring the tableview background to the front of the tableview rows?

// ATableViewController embedded in a NavigationController with UITabBar

- (void)viewWillAppear:(BOOL)animated
{
    // NSLog(@"dateViewDidLoad %f", [[NSDate date] timeIntervalSince1970]);
    [super viewWillAppear:animated];

    NSUserDefaults *textDef = [NSUserDefaults standardUserDefaults];
    NSString *userName = [textDef stringForKey:@"userName"];

    if (userName == nil) {
        SignUpViewController *signup = [[SignUpViewController alloc]initWithNibName:@"SignUpViewController" bundle:nil];
        [signup setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
        [self presentViewController:signup animated:YES completion:nil];

        // Display message you need to sign in to view this content
    } else {
        // Proceed and display the rows
    }
}
1
please share relevant code - meda
Did you separate the UITableView class? or you just create a TableView inside the other class and you set the tableView to delegate ? That will definitely call and create tableView, try to separate the class. - Frans Raharja Kurniawan
@meda My code is fairly complex to copy over, my logic is pretty simple in what I am looking for. Just need to display a message like Apple does if the user is not logged in. Something like "You need to log in to use this feature." - user3817163
@Bejibun in this scenario, I am subclassing UIViewController with <UITableViewDataSource,UITableViewDelegate,UISearchDisplayDelegate,UISearchBarDelegate> I also updated my description as it was misleading. - user3817163

1 Answers

0
votes

If you use a UITableViewController you could "abuse" tableHeaderView of the tableView, by resizing it to full screen and disabling the dataSource methods and scrolling.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if (isLoggedIn) {
        self.tableView.tableHeaderView = nil;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.scrollEnabled = YES;
        [self.tableView reloadData];
    }
    else {
        if (!self.tableView.tableHeaderView) {
            UIView *viewToDisplayIfNotLoggedIn = [[UIView alloc] initWithFrame:self.view.bounds];
            viewToDisplayIfNotLoggedIn.backgroundColor = [UIColor redColor];
            self.tableView.tableHeaderView = viewToDisplayIfNotLoggedIn;
        }
        self.tableView.delegate = nil;
        self.tableView.dataSource = nil;
        self.tableView.scrollEnabled = NO;
        [self.tableView reloadData];
    }
}

If you are using a normal UIViewController it's even easier than that, just hide the tableView and show another UIView:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if (isLoggedIn) {
        self.tableView.hidden = NO;
        self.viewToDisplayIfNotLoggedIn.hidden = YES;
    }
    else {
        self.tableView.hidden = YES;
        if (!self.viewToDisplayIfNotLoggedIn) {
            self.viewToDisplayIfNotLoggedIn = [[UIView alloc] initWithFrame:self.view.bounds];
            self.viewToDisplayIfNotLoggedIn.backgroundColor = [UIColor redColor];
            [self.view addSubview:self.viewToDisplayIfNotLoggedIn];
        }
        self.viewToDisplayIfNotLoggedIn.hidden = NO;
    }
}