Change text of label in ios camera overlay UIPickercontroller - ios

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

Related

UITextView keep text style

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.

Label missing properties after creation via method

Have been searching alot for this, but I think I'm doing something stupidly wrong. I have a lot of labels on my second screen, and they all have the same properties. That's why I create them via a method.
In ViewDidLoad I do this:
[self screenTwoLabelMaker:firstNameLabel withFrame:CGRectMake(30, 200, 200, 40) withText:#"First Name"];
That method is this one:
- (UILabel *)screenTwoLabelMaker:(UILabel *)sender withFrame:(CGRect)frame withText:(NSString *)text
{
sender = [[UILabel alloc] init];
sender.text = text;
sender.frame = frame;
sender.font = [UIFont systemFontOfSize:labelFontSize];
sender.textColor = [UIColor grayColor];
[self.scrollView addSubview:sender];
return sender;
}
Then I do this:
NSLog(#"firstNameLabel x: %f y:%f w:%f h:%f", firstNameLabel.frame.origin.x, firstNameLabel.frame.origin.y, firstNameLabel.frame.size.width, firstNameLabel.frame.size.height);
But the result is this:
MyApp[9608:60b] firstNameLabel x: 0.000000 y:0.000000 w:0.000000 h:0.000000
The weird thing is that the label is being put on the screen on the right place with the right text. So everything should be sunny and warm, no? Well, I also call a similar method for buttons, where there is a tag included. That tag is never active on the button (nil), so further programming for that button is killing me. For now I create them all by hand. Do you guys know what I'm doing wrong?
Your method returns your new label, so you need to assign it to the variable that you want to hold the reference. You don't need to pass in the variable to the method as the first thing you do is overwrite it. Use the following code -
firstNameLabel = [self screenTwoMakeLabelwithFrame:CGRectMake(30, 200, 200, 40) text:#"First Name"];
- (UILabel *)screenTwoMakeLabelwithFrame:(CGRect)frame text:(NSString *)text
{
UILabel newLabel = [[UILabel alloc] init];
newLabel.text = text;
newLabel.frame = frame;
newLabel.font = [UIFont systemFontOfSize:labelFontSize];
newLabel.textColor = [UIColor grayColor];
[self.scrollView addSubview:newLabel];
return newLabel;
}

UIActivityIndicator as long as to implement function

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.

Change navigation controller title after a few seconds

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;
}

iOS Programmatically Creating a View

I am trying to create a new UIView programmatically from one of my .m files and then return to my existing view after 5 seconds. It seems that my logic is off because this is not doing what I am wanting. My code is below.
UIView *mainView = self.view;
UIView *newView = [[UIView alloc] init];
newView.backgroundColor = [UIColor grayColor];
self.view = newView;
sleep(5);
self.view = mainView;
It seems that it is just going to sleep for 5 seconds and then not doing anything.
I want to do the following,
Store starting view
Create new view
Show gray view
Wait 5 seconds
Show my original view
Where am I going wrong? I feel like it has to be my logic or I am missing a key part of these steps.
Thanks for any help! :)
First of all DO NOT USE sleep(). You should use performSelector:withObject:afterDelay: method. Something like this:
-(void)yourMethodWhereYouAreDoingTheInit {
UIView *mainView = self.view;
UIView *newView = [[UIView alloc] init];
newView.backgroundColor = [UIColor grayColor];
self.view = newView;
[self performSelector:#selector(returnToMainView:)
withObject:mainView
afterDelay:5.0];
}
-(void)returnToMainView:(UIView *)view {
//do whatever after 5 seconds
}
Using GCD yields far more readable code, but ultimately its a matter of preference.
// Create grayView as big as the view and add it as a subview
UIView *grayView = [[UIView alloc] initWithFrame:self.view.bounds];
// Ensure that grayView always occludes self.view even if its bounds change
grayView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
grayView.backgroundColor = [UIColor grayColor];
[self.view addSubview:grayView];
// After 5s remove grayView
double delayInSeconds = 5.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[grayView removeFromSuperview];
});
- (void)showBanner {
UIView *newView = [[UIView alloc] initWithFrame:self.view.bounds];
newView.backgroundColor = [UIColor grayColor];
[self.view addSubview:newView];
[newView performSelector:#selector(removeFromSuperView) withObject:nil afterDelay:5.0f];
}
Very preliminary, but should work

Resources