In an iOs app I am using a UINavigationController. On start-up this will have a title set but after a few seconds (in response to some data from a server) this title will need to change.
I tried:
self.title = #"new title";
self.navigationController.title = #"new";
self.navigationItem.title- #"new";
Nothing works... any suggestions?
self.title = #"new";
This might set again after you have to got response from server in the method which is calling to get response.
self.navigationItem.title = #"Old title";
// after some time
double waitToUpdate = .5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(waitToUpdate * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
self.navigationItem.title = #"New title";
});
change UINavigationController title for each view is easy enough. you just need to change the value of self.navigationItem.title. If you wan't to change it with some delay, you can use performSelector:withObject:afterDelay. For example :
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self performSelector:#selector(changeTitle:) withObject:#"New Title" afterDelay:2.]; // two seconds delay
}
- (void)changeTitle:(NSString *)title {
self.navigationItem.title = title;
}
Related
Can someone help me format some code? I want to show an image on my storyboard and after 5 seconds change it to a new image on screen. Heres what I have so far. This code changes a string after 5 seconds successfully.
double delayInSeconds1 = 5.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds1 * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
hello.text = [NSString stringWithFormat:#"welcome"];
});
double delayInSeconds2 = 5.0;
dispatch_time_t popTime1 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds2 * NSEC_PER_SEC));
dispatch_after(popTime1, dispatch_get_main_queue(), ^(void){
hello.text = [NSString stringWithFormat:#"hello there"];
});
How can I set up my .h file so that hello.text is something like image=[UIImage imageNamed:#"image1"] Should I use a UIImageView outlet or an UIImage? So far, nothing appears or changes. How do i easily initiate a custom image sequence? The funny thing is I've gotten this to work before but forgot what I did. always save your projects even the dumb ones
You could use the animationImages property of an UIImageView:
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 141, 219)];
image.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
nil];
image.animationDuration = 5;
image.animationRepeatCount = 0; // infinitely
[image startAnimating];
Example from a blank XCode project :
In ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *image;
And in ViewController.m:
#implementation ViewController
#synthesize image = _image;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_image.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"jeu0_icon1.png"],
[UIImage imageNamed:#"jeu0_icon2.png"],
nil];
_image.animationDuration = 5;
_image.animationRepeatCount = 0; // infinitely
[_image startAnimating];
}
And it works (with my 2 png assets well added to the project and the UIImageView added in the stb, of course...
I'm trying to change though a timer in a different method the text of a label I created programmatically in a UIView overlay that goes on a UIImagePickerviewController, but of course when I try to change the text in this way
labelname.text = #"TEST";
I get the error "use of undeclared identifier labelname"
How can I refer to that specific label? Should I create a new label each time the timer ticks?
I tried to declare it in the .h file, but I'm guessing i'm just creating a different label...any ideas?
Just Pass the Label As a whole to the Timer Function:
Say Your Label is defined in ViewDidLoad like this :
- (void)viewDidLoad
{
//Do Something here
UILabel * label = [[UILabel alloc]
initWithFrame:CGRectMake(xcoordinateLabel, ycoordinateLabel, width, height)];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.textColor=[UIColor whiteColor];
label.text = #"TEST";
[self.view addSubview:label];
[self changeLabelText:label];
}
Where your changeLabelText is defined as this with some delay:
- (void) changeLabelText:(UILabel *)label
{
//Changing Values after 10s Delay
double delayInSeconds = 10.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
label.text = #"TESTING Change";
});
}
Your Timer function can replace changeLabelText
To allow user interaction with links I use code like this:
UITextView *t1 = [UITextView new];
[self.view addSubview:t1];
t1.frame = self.view.frame;
t1.font = [UIFont systemFontOfSize:16];
t1.text = #"go to http://apple.com";
t1.dataDetectorTypes = UIDataDetectorTypeLink;
t1.editable = NO;
But if I need to set new text, it reuse style:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
t1.text = #"test";
});
Will show "test" like link (blue color) but without link behaviour (go to link, context menu). I didn't find in documentation why this is legal.
As I understand the only way to "fix" this is reset attributes like font, text color to initial values.
I found this way to fix:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
t1.dataDetectorTypes = UIDataDetectorTypeNone;
t1.dataDetectorTypes = UIDataDetectorTypeLink;
t1.text = #"another text with http://link.com link";
});
But maybe there is more correct/elegant solution.
I want to use UIActivityIndicator on a function.
I'm implementing some Core Filters, some of which take almost a second to implement. I want that UIActivityIndicator to start and stop according the function.
I looked up online, but it's mostly using a timer. So that would make it hard-wired and not based on how long it actually takes to implement the function.
Can someone tell me a small example how I can do that ?
Declare ActivityIndicator
#property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
then,
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
// Do any additional setup after loading the view, typically from a nib.
CGRect frame = CGRectMake (120.0, 185.0, 80, 80);
self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:frame];
[self.view addSubview:self.activityIndicatorView];
}
using this code you can start and stop the activityIndicator
[self.activityIndicatorView startAnimating]; //start
[self.activityIndicatorView stopAnimating]; //stop
all UI animation and navigation would be performed once the method execution is over. So if you want such functionality go for timer or dispatch queue.
as follows
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithFrame:self.window.frame];
[activity startAnimating];
//Your methods to be executed
double delayInSeconds = 0.01;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[activity stopAnimating];
});
in this method execution is kept in a queue after the execution of current method, method written dispatch_time_t will be executed.
If UIRefreshControl is started programmatically,(beginRefreshing and then endRefreshing ) when it is not visible, then there would be a continuous flicker in the UIRefreshControl animation on next manual refresh.
Add this method in UITableViewController subclass and do a pull to refresh after 2 seconds and you will see the flicker
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIRefreshControl * refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl = refreshControl;
[self.refreshControl beginRefreshing];
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self.refreshControl endRefreshing];
});
}
How to avoid this flicker?
Put your code in the viewDidLoad or viewWillAppear instead of viewDidAppear.
Also, can you tell me the reasoning behind putting the code inside the viewDidAppear method, rather than viewDidLoad or viewWillAppear?