I created a method that sets the title of a button based on a value.
This method needs to be called when opening the viewController and maybe refreshed when the controller appears again.
So i created the method and I called that method in viewDidLoad and viewDidApper but it seems to be called only when I change page and turn back to the view controller.
Why?
My code is
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self controlloRichieste];
......
}
-(void)viewDidAppear:(BOOL)animated{
[self controlloRichieste];
}
-(void)controlloRichieste{
//Numero richieste di contatto
NSString *numeroRichieste = #"1";
if([numeroRichieste isEqual:#"0"]){
[_labelRequestNumber setTitle:#"Nessuna" forState:UIControlStateNormal];
} else {
_labelRequestNumber.titleLabel.text = numeroRichieste;
_labelRequestNumber.tintColor = [UIColor redColor];
}
//Fine Numero richieste di contatto
}
You can also move that code to viewWillAppear so that it gets called each time it appears.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self controlloRichieste];
}
I see the problem now, try the other way around
-(void)controlloRichieste{
//Numero richieste di contatto
NSString *numeroRichieste = #"1";
if([numeroRichieste isEqual:#"0"]){
[_labelRequestNumber setTitle:#"Nessuna" forState:UIControlStateNormal];
} else {
_labelRequestNumber.tintColor = [UIColor redColor];
[[_labelRequestNumber titleLabel]setText:numeroRichieste];
}
//Fine Numero richieste di contatto
}
Change set the button color, before you change its titleLabel's text
I created a demo PROJECT for you, hope it's helpful!
When you open view first time the viewDidLoad is called and the viewDidAppeare.
The viewDidAppeare is called every time when the view is opened, when you push or present other view controller and go back to the maine one viewDidAppeare is called.
You should call:
[super viewDidAppear:animated];
The viewDidLoad is called just when the view is loaded and after that when it's deallocated and it needs to be allocated again. So mostly when you push or present other view controller and go back to the maine one viewDidLoad is not called.
Related
I have two views in my app and a plist file to store some values.
In the first view I've created a button called frequenciesButton that opens the second view and another button to restore the default values.
In the second view there is a pickerView and a "Done" button.
On the .m of the first view:
- (void)viewDidLoad {
[super viewDidLoad];
//
self.gameSettings = [[NSMutableDictionary alloc] initWithContentsOfFile:gameSettingsFilePath];
}
-(void)viewWillAppear:(BOOL)animated {
[self refreshView];
}
- (void)refreshView {
[self.frequenciesButton setTitle:[NSString stringWithFormat:#"%# hz and %# hz", [self.gameSettings objectForKey:#"freq-freq1"], [self.gameSettings objectForKey:#"freq-freq2"]] forState:UIControlStateNormal];
...
}
- (IBAction)setDefaultValues:(UIButton *)sender {
[self.gameSettings setValue:#880 forKey:#"freq-freq1"];
[self.gameSettings setValue:#1122 forKey:#"freq-freq2"];
...
[self.gameSettings writeToFile:gameSettingsFilePath atomically:YES];
[self refreshView];
}
When the first view is loaded, the button title is changed to the default values stored in the gameSettings dictionary. The method setTitle: works.
When I click on the frequenciesButton it opens the second view with the pickerView, I select the two new values for the freq-freq1 and freq-freq2 and it saves to the plist file on done button.
The problem is that the frequenciesButton title is not changed when the second view is dissmissed and the first view appears. The refreshView method is called but the button setTitle: does not work.
In this case, if I go back one screen, and return to this view, the button title is updated.
And when I click on defaultValuesButton, the frequenciesButton title changes. The method setTitle: also works.
Any ideas of what must be happening?
HaHA! I love that you added a link to your project.
SO!! The problem was that you have separate properties in each view to hold the data from the saved plist file, self.settings. This is fine, don't mesh them together. The requirement you had to do with this, when switching views, is to keep the ivar or properties updated as the data updates too :D
Here is how I fixed the problem:
- (void)viewWillAppear:(BOOL)animated {
self.settings = [NSMutableDictionary dictionaryWithContentsOfFile: filePath];
[self updateView];
}
I checked out the file and that was updated, but the dictionary in the TestViewController.h was not updated
I hope this was the problem :)
One problem there, not sure if it will fix it, is the fact that you have used ViewWillAppear incorrectly, you have this:
-(void)viewWillAppear:(BOOL)animated {
[self refreshView];
}
but it should be this:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self refreshView];
}
You need to invoke "[super viewWillAppear:animated];" or you will have side effects, fix that first and see what happens.
My Application is getting crashed with the following error.
-[PreviewViewController applicationWillSuspend]: message sent to deallocated instance 0x1806d9e0
My application have two view controllers one is HomeViewController and other one is PreviewViewController.
In home view controller i am displaying a table view. When selecting the row of table view i am presenting the preview view controller.
I selected one row then preview view controller is presented.
PreviewViewController *previewController = [[PreviewViewController alloc]initWithPreviewImage:[[kfxKEDImage alloc] initWithImage:imgCaptured] withSourceofCapture:_typeOfCapture typeOfDocumentCaptured:PHOTO];
[self presentViewController:previewController animated:YES completion:nil];
Dismissed the preview view controller.
[self dismissViewControllerAnimated:YES completion:nil];
Application goes into background then it is not crashed.
I selected two rows one after another. Application goes into background then it is crashed. I don't know why it is behaving like that. If anyone know the solution please tell me.
Thanks In Advance
I had this problem, it was caused by someone overriding 'dealloc' in a UIViewController category.
https://github.com/taphuochai/PHAirViewController/issues/13
#chrishulbert
Remove this:
- (void)dealloc
{
self.phSwipeHander = nil;
}
Replace dealloc with this:
/// This is so that phSwipeGestureRecognizer doesn't create a swipe gesture in *every* vc's dealloc.
- (BOOL)phSwipeGestureRecognizerExists {
return objc_getAssociatedObject(self, SwipeObject) ? YES : NO;
}
- (void)ph_dealloc
{
if (self.phSwipeGestureRecognizerExists) {
self.phSwipeHander = nil;
}
[self ph_dealloc]; // This calls the original dealloc.
}
/// Swizzle the method into place.
void PH_MethodSwizzle(Class c, SEL origSEL, SEL overrideSEL) {
Method origMethod = class_getInstanceMethod(c, origSEL);
Method overrideMethod = class_getInstanceMethod(c, overrideSEL);
if (class_addMethod(c, origSEL, method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod))) {
class_replaceMethod(c, overrideSEL, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
} else {
method_exchangeImplementations(origMethod, overrideMethod);
}
}
/// Swizzle dealloc at load time.
+ (void)load {
SEL deallocSelector = NSSelectorFromString(#"dealloc"); // Because ARC won't allow #selector(dealloc).
PH_MethodSwizzle(self, deallocSelector, #selector(ph_dealloc));
}
I am using setNeedsDisplay on my GUI, but there update is sometimes not done. I am using UIPageControllView, each page has UIScrollView with UIView inside.
I have the following pipeline:
1) application comes from background - called applicationWillEnterForeground
2) start data download from server
2.1) after data download is finished, trigger selector
3) use dispatch_async with dispatch_get_main_queue() to fill labels, images etc. with new data
3.1) call setNeedsDisplay on view (also tried on scroll view and page controller)
Problem is, that step 3.1 is called, but changes apper only from time to time. If I swap pages, the refresh is done and I can see new data (so download works correctly). But without manual page turn, there is no update.
Any help ?
Edit: code from step 3 and 3.1 (removed _needRefresh variables pointed in comments)
-(void)FillData {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *stateID = [DataManager ConvertStateToStringFromID:_activeCity.actual_weather.state];
if ([_activeCity.actual_weather.is_night boolValue] == YES)
{
self.contentBgImage.image = [UIImage imageNamed:[NSString stringWithFormat:#"bg_%#_noc", [_bgs objectForKey:stateID]]];
if (_isNight == NO)
{
_bgTransparencyInited = NO;
}
_isNight = YES;
}
else
{
self.contentBgImage.image = [UIImage imageNamed:[NSString stringWithFormat:#"bg_%#", [_bgs objectForKey:stateID]]];
if (_isNight == YES)
{
_bgTransparencyInited = NO;
}
_isNight = NO;
}
[self.contentBgImage setNeedsDisplay]; //refresh background image
[self CreateBackgroundTransparency]; //create transparent background if colors changed - only from time to time
self.contentView.parentController = self;
[self.contentView FillData]; //Fill UIView with data - set labels texts to new ones
//_needRefresh is set to YES after application comes from background
[self.contentView setNeedsDisplay]; //This do nothing ?
[_grad display]; //refresh gradient
});
}
And here is selector called after data download (in MainViewController)
-(void)FinishDownload:(NSNotification *)notification
{
dispatch_async(dispatch_get_main_queue(), ^{
[_activeViewController FillData]; //call method shown before
//try call some more refresh - also useless
[self.pageControl setNeedsDisplay];
//[self reloadInputViews];
[self.view setNeedsDisplay];
});
}
In AppDelegate I have this for application comes from background:
-(void)applicationWillEnterForeground:(UIApplication *)application
{
MainViewController *main = (MainViewController *)[(SWRevealViewController *)self.window.rootViewController frontViewController];
[main UpdateData];
}
In MainViewController
-(void)UpdateData
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(FinishForecastDownload:) name:#"FinishDownload" object:nil]; //create selector
[[DataManager SharedManager] DownloadForecastDataWithAfterSelector:#"FinishDownload"]; //trigger download
}
try this:
[self.view performSelectorOnMainThread:#selector(setNeedsLayout) withObject:nil waitUntilDone:NO];
or check this link:
http://blackpixel.com/blog/2013/11/performselectoronmainthread-vs-dispatch-async.html
setNeedsDisplay triggers drawRect: and is used to "redraw the pixels" of the view , not to configure the view or its subviews.
You could override drawRect: and modify your labels, etc. there but that's not what it is made for and neither setNeedsLayout/layoutSubviews is.
You should create your own updateUI method where you use your fresh data to update the UI and not rely on specialized system calls meant for redrawing pixels (setNeedsDisplay) or adjusting subviews' frames (drawRect:).
You should set all your label.text's, imageView.image's, etc in the updateUI method. Also it is a good idea to try to only set those values through this method and not directly from any method.
None of proposed solutions worked. So at the end, I have simply remove currently showed screen from UIPageControllView and add this screen again. Something like changing the page there and back again programatically.
Its a bit slower, but works fine.
I've got this really weird bug on my code, and I have no idea on how to solve it.
I'm using the stroryboard scheme as shown on the picture attached. One navigation controller, that performs a push segue programatically, and, when clicking the "Find" button, the "findsegue" is supposed to be performed.
So, the problem is: Randomly, the view is not loaded. viewDidLoad is executed, but nothing changes on my screen. Some other times, it all works like a charm.
Here is the code that is called when the "Find" button is pressed:
- (IBAction)encontreBtn:(id)sender {
NSLog(#"Segue is going on");
[self performSegueWithIdentifier:#"findsegue" sender:sender];
}
#"Segue is going on" is always printed.
And that's the viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"DIDLOAD");
// Do any additional setup after loading the view.
_topBar.font = [UIFont fontWithName:#"LibelSuit-Regular" size:23];
_topBar.textAlignment = NSTextAlignmentCenter;
_tableView.dataSource = self;
_tableView.delegate = self;
}
#"DIDLOAD" does print as well.
As a note, viewDidAppears runs as well.
i have a problem, i want set text of a UILabel or UItextView or another IBOUTLET objects,but i can do it only in the viewwillappear and viewdidload method, if i set text in another method in the code don't change nothing, here is an example:
i have a method that retrieve string from another class, and then i want set this string in my uilabel:
- (void)setDetailItem:(id)newDetailItem
{
if (detailItem != newDetailItem) {
NSLog(#"SetDetailItem");
[detailItem release];
detailItem = [newDetailItem retain];
detailString = [[FindStringClass alloc] init];
// Update the view.
detailItem = [detailString searchStringFor:detailItem.name];
if (detailItem) {
//
NSLog(#"setDetail: %#",detailItem.stringName);
[self configureView];
}
}
}
- (void)configureView {
NSLog(#"configure view: %#",detailItem.stringName);
mySerialTitle.text = detailItem.stringName;
}
the NSLog work, and i can see my string in the console, but the text in view don't change, instead if i set a simple text in the ViewWillAppear or viewDidLoad method work, so the connection with the IBOOutlets are right, like this:
- (void)viewDidLoad
{
mySerialTitle.text = #"CIAO";
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
This is the call function where i call the setDeatItem from another view:
self.searchResultViewController.detailItem = [myArray objectAtIndex:[indexPath row]];
[self.navigationController pushViewController:self.searchResultViewController animated:YES];
anyone can help me?
EDIT:
This is the NSLog where the method are called:
2012-03-19 12:51:02.617 TestApp[292:b603] SetDetailItem
2012-03-19 12:51:06.161 TestApp[292:b603] setDetail: Chicken
2012-03-19 12:51:06.162 TestApp[292:b603] configure view: Chicken
2012-03-19 12:51:06.175 TestApp[292:b603] View DidLoad
i call the searchviewcontroller view from a Tableviewcontroller i press on a row and then call the new view and pass the attribute.
viewDidLoad is called after the view is loaded into memory. See viewDidLoad in the apple docs. After the view is loaded into memory and right before it appears, viewWillAppear is called.
You cannot change the properties of the view before it is loaded. So, examine your code paths (and maybe even log out in those methods and the viewDidLoad and viewWillAppear method) and see if you're setting those properties before viewDidLoad is called (log output will make it easy to see).
From viewDidLoad docs:
This method is called after the view controller has loaded its view
hierarchy into memory. This method is called regardless of whether the
view hierarchy was loaded from a nib file or created programmatically
in the loadView method. You usually override this method to perform
additional initialization on views that were loaded from nib files.
You're log statements that you added makes it clear that you are manipulating the views before they are loaded. As you can see, setDetail is called before viewDidLoad.
2012-03-19 12:51:02.617 TestApp[292:b603] SetDetailItem
2012-03-19 12:51:06.161 TestApp[292:b603] setDetail: Chicken
2012-03-19 12:51:06.162 TestApp[292:b603] configure view: Chicken
2012-03-19 12:51:06.175 TestApp[292:b603] View DidLoad
If you want to set the data on the view you're pushing, you have a couple options.
Call setDetail but in setDetail only set iVar data - don't manipulate views. Then, in viewDidLoad/WillAppear, read the iVar data and manipulate the views.
Use delegates to have the view you're pushing call back to the launching view. See What exactly does delegate do in xcode ios project?
- (void)setDetailItem:(id)newDetailItem
{
if (detailItem != newDetailItem) {
NSLog(#"SetDetailItem");
[detailItem release];
detailItem = [newDetailItem retain];
detailString = [[FindStringClass alloc] init];
// Update the view.
detailItem = [detailString searchStringFor:detailItem.name];
if (detailItem) {
//
NSLog(#"setDetail: %#",detailItem.stringName);
[self configureView];
}
}
}
the above function wherever you are calling it form..i don't know.. make sure you call it after
[self.navigationController pushViewController:self.searchResultViewController animated:YES];
if you call it before this.. then your view has not loaded properly and setting text to a label won't work..