Reload UIViewController in "viewDidLoad" - ios

I change the value of 2 UILabels in my "viewDidLoad" method, but I need the view to refresh after that in order to display the values. As it currently stands, the UILabels display the value of the previously selected cell. I need to do the refresh right after I change the labels' values. The "setNeedsDisplay" method is not doing the job.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_nameLabel.text = _selectedLocation.name;
_addressLabel.text = _selectedLocation.address;
[self.view setNeedsDisplay];
}

Based on your comments, I think you are trying to do something like:
- (void)updateLabelTexts {
_nameLabel.text = _selectedLocation.name;
_addressLabel.text = _selectedLocation.address;
}
and wherever you are changing the _selectedLocation values:
//Just an example
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
_selectedLocation = _yourLocationsArray[indexPath.row];
//now you call your update method
[self updateLabelTexts];
}
The point is that you have to call [self updateLabelTexts]; just after you update the values.

A very stupid bug. Turns out when I made the segue to transition into the next view, I actually dragged it from a physical cell on to the destination controller. However, I should've simply connected the sending uiview controller to the destination viewcontroller with the segue, and then manually handled the transition. That fixed it, so there's no need to "refresh or reload" the UIView as I was trying to do.

Related

Popover in UITableViewcell not coming immediately .

i have a dynamic tableviewcontroller SideBarTableViewController.m/.h .
Have a pop up view controller - popUpViewController.m/.h
Have subclassed a cell of SideBarTableViewController -> RegisterTableViewCell.m/.h and added a button outlet from the cell to it.
Have connected the cell to popUpViewController in storyboard using "present As popover" segue and the segue is given identifier "popover". The anchor point in storyboard has been set to Tableview for now , changing it later in preparesegue .
RegisterTableViewCell.h
#property (weak, nonatomic) IBOutlet UIButton *PopoverAnchorButton;
SideBarTableViewController.m
#import "RegisterTableViewCell.h"
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:#"popover"] && segue.destinationViewController.popoverPresentationController){
UIPopoverPresentationController *popController = segue.destinationViewController.popoverPresentationController;
popController.sourceView = sender;
RegisterTableViewCell *cell = [[RegisterTableViewCell alloc]init];
segue.destinationViewController.popoverPresentationController.sourceRect = CGRectMake(cell.PopoverAnchorButton.frame.size.width/2, cell.PopoverAnchorButton.frame.size.height, 0, 0);
popController.delegate = self;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
RegisterTableViewCell *regcell = [[RegisterTableViewCell alloc]init];
[regcell.PopoverAnchorButton addTarget:self action:#selector(PresentPopover:) forControlEvents:UIControlEventTouchUpInside];
// Configure the cell...
return cell;
}
-(void) PresentPopover:(UIButton *)sender
{
NSLog(#"present popover called ");
[self performSegueWithIdentifier:#"popover" sender:sender];
}
popUpViewController.m
- (IBAction)closeview:(id)sender {
[self.view removeFromSuperview];
}
Problem:
The popup is displayed from the left end of the cell, but wat i see , there is a delay in popup presentation . Normally , first click in the cell, popup comes up as expected ,
second click -> little delay ,
third -> little more delay ... (delay not always increasing though, but fixed 6-7 seconds )
let the ipad be idle for say 6 seconds , and again click in the cell, popup shows immediately .
double clicking(2 taps ) in the cell displays it immediately but with a warning :
2016-07-11 22:53:05.462 Player-app_03[1947:590814] Warning: Attempt to present <UINavigationController: 0x125026400> on <SideBarTableViewController: 0x124d258f0> which is already presenting (null)
What is happening here ? i am clueless. Anybody please help.
The problem is likely to be found in the actions of the segue.destinationViewController, which, from the OP code, appears itself to handle another view controller. To get a handle on this, omit the prepareForSegue logic that you have, and omit anything in the destination vc's viewDidLoad or viewWillAppear that is related to vc presentation.
The segue.destinationViewController is the view controller that's going to get presented. Anything that messes with that is asking for trouble.
The increasing lag time is very likely caused by the action (inaction, rather) in closeview, which is merely removing the popup view controller's view, leaving the view controller itself around indefinitely. The proper action in that method is to call:
[self dismissViewControllerAnimated:YES completion:nil];
Also, please note that any place you directly allocate a table view cell (done twice in the OP code), is indicative of at least a misunderstanding, and probably a mistake.

Is it possible to switch view to tableview from same view?

I have a viewcontroller.xib which contains View, buttons,toolbarbutton, text box and a tableview. When I load the initial screen comes without table view which is fine. Now when I click on a toolbarbutton say, viewtable, I want the view to move to tableview. I have filled my tableview data with some default objects like this:
- (void)viewDidLoad
{
tableData = [[NSArray alloc] initWithObjects:#"object1",#"object2",#"object3",#"object4", nil];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"My Cell"];
if(cell==nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"My Cell"];
}
cell.textLabel.text=[tableData objectAtIndex:indexPath.row];
return cell;
}
So when I click on toolbar view button it should show tableview with toolbar button which also has a back button so when I click on that it should hide the table view and show the initial view. Is it possible to do all this in single xib? I can achieve the result if I create another xib and simply transfer control over to that xib but I wanted to know if its possible do this without creating a second xib file. And also for navigation I can use navigation controller but I want to check and see if its possible to use toolbar to transfer the control. Thanks.
First check if your table view is inside your view, if not put it inside and set delegate of datasource to file owner, then in your view table method write this code
-(void)viewTable
{
self.tableView.hidden = NO;
self.viewToolbar.hidden=YES;
}
On your back button code in toolbar write
-(void)goback
{
self.tableView.hidden = YES;
self.viewToolbar.hidden=NO;
}
If you don't need animation then you can do the following
Get a handle of tableView in your interface like this:
#property(nonatomic,assign)IBOutlet UITableView *tableView;
Hide your table view in initially ( like in viewDidLoad method )
-(void)viewDidLoad
{
[super viewDidLoad];
self.tableView.hidden = YES;
}
Then in the method called by your toolbar's button do the following
-(void)on_click_toolbar_button
{
self.tableView.hidden = !self.tableView.hidden;
//This will keep toggling the table view from hidden to shown & vice-versa.
}
you could use the hidden property to achieve that. Put these in the appropriate ibaction methods.
_tableView.hidden = Yes;
_tableView.hidden = No;
I'd highly recommend to do this in two separate XIBs. The first should contain a UIViewController (your initial view) and the second a UITableViewController (your table view) class. Both should be handled by a UINavigationController - don't fight the API and try your own hacks if it's not necessary. The mentioned controller classes give you everything you need out of the box.
Well this is not recommended but you can do this by removing and adding tableview..

UITableView reloadData doesn't work

I've read all similar questions and tried all suggestions, still nothing. Maybe someone can spot my flaw.
My view controller is initiated from another view controller, by one of two buttons. Button taps send NSNotification (with attached arrays), and this view controller anticipates this notification and then calls this method:
- (void)addContentToArray:(NSNotification *)aNotification {
array = [NSArray arrayWithArray:[aNotification object]];
([array count] == 6) ? (category = YES) : (category = NO);
[myTableView reloadData];
NSLog(#"%d", [array count]);
NSLog(#"%#", myTableView);
}
The method gets called every time, I can see that from changing array count. Here notification object is the array passed from previous view controller, and I assign these objects to my local array property - this is my UITableView source. So what I do is I try to reuse the UITableView to display elements of whatever array is being passed. And it works nicely for the first array passed (whichever first).
When I tap the second button, the new array is passed successfully (as mentioned before, I know that from log of [array count] which is different: 3 vs 6 objects in different arrays). However, what is not happening is that UITableView does not refresh (although the values passed when I select a row in the table are from the correct arrays, even though wrong values are displayed).
Here are UITableView data source methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Identifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Identifier"];
}
cell.textLabel.text = [[array objectAtIndex:indexPath.row] objectForKey:#"name"];
if (category) {
cell.detailTextLabel.text = [[array objectAtIndex:indexPath.row] objectForKey:#"description"];
}
return cell;
}
So, what am I doing wrong?
A few other considerations that might help:
NSLog(#"%#", myTableView); returns (null), which is a bit worrying. myTableView here is UITableView from my nib file, which is correctly connected to the view controller, declared as property and synthesized
The view controller in question is a rightViewController of the PKRevealController, so when it is called repeatedly, viewWillAppear method is called, but not viewDidLoad (although, as I already mentioned, addContentToArray: method is being called every time as well)
Also, for those somewhat familiar with PKRevealController - when I try and log focusedController from my view controller, it says that frontViewController - the one that moves to reveal my view controller - is the one that is focused. Can that be the reason why myTableView is (null)?
I'd be grateful for any insight and help!
Need more code, that part where you created and call Myviewcontroller's addContentToArray method.
I think you used release code there for Myviewcontroller's object, try once with hide that part.
I managed to solve the issue by editing initWithNibName method (old line commented out)
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
//self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
self = [super initWithNibName:#"PurposeCategoryViewController" bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
Apparently, it did have something to do with the fact that my view controller (called PurposeCategoryViewController) was not the top/focused view controller in PKRevealController hierarchy. So, I just needed to specifically indicate my nib file.
What value are you returning in this delegate method:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
make sure it is 1

ViewDidLoad is not called for the second time Xcode

I have a tableViewController from which i navigate to UIViewController. To save the value of indexPath.row I have used an integer and based on that integer action is performed in UIViewController. Problem is that when I press the back button and selects another index of table, It navigates to UIViewContoller but the action performed is the first one. the ViewDidLoad is not called for the second time.
here is my code.
TableView Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.iaRecipieViewController) {
self.iaRecipieViewController = [[[iRecipieViewController alloc] initWithNibName:#"iRecipieViewController" bundle:nil] autorelease];
}
if (indexPath.row == 0) {
self.iaRecipieViewController.myLableArray =labelSoupArray ;
}
if (indexPath.row == 1) {
self.iaRecipieViewController.myLableArray =labelSoupArray ;
}
if (indexPath.row == 2) {
self.iaRecipieViewController.myLableArray =labelSoupArray ;
}
// custom string is an NSinteger
self.iaRecipieViewController.customString = indexPath.row;
NSLog(#" int %i",self.iaRecipieViewController.customString);
NSLog(#"index path %i ", indexPath.row) ;
[self.navigationController pushViewController:iaRecipieViewController animated:YES];
[detailTable reloadData];
}
UIViewController Code.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
myLabel.text = [self.myLableArray objectAtIndex:customString];
}
Use viewWillAppear:(BOOL)animated
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
myLabel.text = [self.myLableArray objectAtIndex:customString];
}
viewDidLoad will only get called when the view is loaded; pushing a new ViewController and then removing it won't force the view to be reloaded. viewWillAppear is called before the view is rendered, so you have an opportunity to do things whenever it becomes the primary view.
ViewDidLoad gets called only once when the view is instantiated. Based on your code, you are going back and forth on this view and as such the view is not instantiated (loaded) everytime but rather it was loaded once and now it is really the view disappearing and appearing as you go back and forward to that view.
Put your code in ViewWillAppear or ViewDidAppear.
The method
(void)viewDidLoad
is run when the view gets loaded.
If your view already loaded(means it's still in the memory),maybe it's gone from the screen. But it doesn't mean that the memory is gone; Just re-Run when the view should load to the memory.

UITableView - Accessing in Code from ViewWillAppear

I would like to manipulate table cells in my UITableView as it comes back from its detail view, to remind the user what row they last selected. The UITableView was created in IB and there currently isn't a #property or custom class for the UITableView or any other handle. Clearly, it exists when, the user is coming back from the detail view and viewWillAppear is called but I don't see any handle in the debugger for my UITableView.
Any way to simply get a handle to the IB tableView object?
// - The Containing ViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSIndexPath *selected = [self.tableView indexPathForSelectedRow];
if(selected){
//Do something stylish with the last-selected row
NSLog(#"Last row selected was: %#",selected);
}
}
I solved this by creating a class member of NSIndexPath * and set it inside didSelectRowAtIndexPath. Then, when viewWillAppear executes, when the user navigates back fram the table view detail, I have the last-selected row so I can easily highlight it or treat it any way I want:
//The Containing View Controller
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if(self.lastSelectedRow){
[self.tableView selectRowAtIndexPath:self.lastSelectedRow animated:YES scrollPosition:UITableViewScrollPositionNone];
}
}

Resources