removeObjectFromSuperView not working with if statement - ios

I have a button that when pressed is supposed to hide the status bar and place text in its spot. Then, when the button is pressed the label is supposed o be removed from the view and the status bar will re-appear. The first part works -- the status bar is hidden and the label is placed on the view, the problem is when I press the button a second time (to remove the text and put back the status bar). The status bar re-appears but the label is not being removed from the view. To achieve this I am using an if statement. I am also using removeObjectFromSuperView which is the thing that is not working.
Here is the code:
- (IBAction)buttonPressed:(id)sender {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 21)];
label.text = #"This is a test";
[label setFont:[UIFont systemFontOfSize:13]];
label.textAlignment = NSTextAlignmentCenter;
if (hidden == NO) {
[UIApplication sharedApplication].statusBarHidden = YES;
[self.view addSubview:label];
hidden = YES;
}else if (hidden == YES) {
[UIApplication sharedApplication].statusBarHidden = NO;
[label removeFromSuperview];
hidden = NO;
}
}
Thanks
Edit: Here is what the problem looks like:

You need to track your label outside of the buttonPressed: method. Right now you are creating a new label every time the button is pressed then removing that same label.
So for example:
#interface MyViewController () {
UILabel *label;
}
- (IBAction)buttonPressed:(id)sender {
if (hidden == NO) {
[UIApplication sharedApplication].statusBarHidden = YES;
label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 21)];
label.text = #"This is a test";
[label setFont:[UIFont systemFontOfSize:13]];
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
hidden = YES;
} else if (hidden == YES) {
[UIApplication sharedApplication].statusBarHidden = NO;
[label removeFromSuperview];
label = nil;
hidden = NO;
}
}
You can also set label = nil; once you remove it from the super view.

Related

How do i disable the editing of a UITextView based on a UISwitch?

I have a UITextView and i want to disable it's 'editable' property if the state of the UISwitch is set to ON.
myTextView = [[UITextView alloc]initWithFrame:CGRectMake(20, 180, 200, 30)];
myTextView.backgroundColor = [UIColor whiteColor];
myTextView.text = #"This is a textView";
[self.view addSubview:myTextView];
mySwitch = [[UISwitch alloc]initWithFrame:CGRectMake(230, 125, 200, 30)];
[self.view addSubview:mySwitch];
I know that i can check the state of a switch using:
if ([mySwitch isOn]) {
myTextView.editable = NO;
} else {
myTextView.editable = YES;
}
The problem is when i run my project the text is not editable, but when i slide the switch the text cannot be edited (which is obvious with above code). I want to know what do i have to do to change the editable property of the UITextView, if i toggle the switch.
UISwitch is a subclass of the UIControl class, which among other things, provides a target-action-based model for receiving updates about the state of the control.
This means that you can use the -[UIControl addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents] method to have your method called when the switch changes its boolean value:
[yourSwitch addTarget:self action:#selector(switchToggled:) forControlEvents:UIControlEventValueChanged]
You can also do this from Interface Builder, by right-clicking on your UISwitch and assigning its action to the appropriate IBAction method:
- (IBAction) switchToggled:(UISwitch*)switchControl {
yourTextView.editable = switchControl.on;
}
You need to add an IBAction and hook it up to the switch method Value Changed
-(IBAction)toggleSwitch:(UISwitch*)sw {
myTextView.editable = sw.on;
NSLog(#"Value Changed");
}
Edit: Code for doing it programmatically rather than via Interface Builder
myTextView = [[UITextView alloc]initWithFrame:CGRectMake(20, 180, 200, 30)];
myTextView.backgroundColor = [UIColor whiteColor];
myTextView.editable = NO;
myTextView.text = #"This is a textView";
[self.view addSubview:myTextView];
mySwitch = [[UISwitch alloc]initWithFrame:CGRectMake(230, 125, 200, 30)];
mySwitch.on = NO;
[mySwitch addTarget:self action:#selector(toggleSwitch:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:mySwitch];
-(void)toggleSwitch:(UISwitch*)sw {
myTextView.editable = sw.on;
}
You have to create an action for the switch which fires when the switch's state has changed. If you are using storyboard, right-drag from the switch to the code (like to create an outlet) and choose action. Inside that function you check whether your switch is now on or off and according to that you make your TextView editable or not
if ([mySwitch isOn]) {
myTextView.editable = NO;
[myTextView resignFirstResponder];
} else {
myTextView.editable = YES;
[myTextView becomeFirstResponder];
}

iOS 8 UILabel text is null issue

I have an issue in the iOS 8 with the UILabel. I am printing a text on the view while the text won't remove from the view if the result1 is bigger than the result2 while it is working fine in the iOS 7. And I have debugged and found that the label1.text is always null before I remove it from the screen. Please where would be my issue?
-(void)warningAlert{
label1 = [[UILabel alloc] initWithFrame:CGRectMake(15, 85, 300, 660)];
label1.backgroundColor = [UIColor clearColor];
label1.textColor=[UIColor redColor];
label1.numberOfLines=0;
[self.view addSubview:label1];
if (result1 < result2)
{
if (printed == NO) {
label1.text = #"“Warning Warning!!”";
printed = YES;
}
}
else{
NSLog(#"check the value %#", label1.text);
[label1 removeFromSuperview];
printed = NO;
}
Instead of adding and removing the label.
Alloc the memory to label only once in viewDidlload
And then Just hide or unhide the label.

iOS UIButton text colour doesn't change after adding subview

I have this function which get called once a button is clicked.
I'm trying to add a view to a clicked button and a view to a un-clicked button.
This is my view's init:
/**
Create the bottom border views
*/
- (void) createBottomBorderViews {
UIButton* button = _albumsTypesButtons[0];
//create layers
_clickedButtonBorder = [[UIView alloc] initWithFrame:CGRectMake(0, 26, button.frame.size.width, 2)];
_unClickedButtonBorder = [[UIView alloc] initWithFrame:CGRectMake(0, 27, button.frame.size.width, 1)];
//set the clicked layer
_clickedButtonBorder.backgroundColor = [UIColor colorWithRed:86/255.0 green:88/255.0 blue:87/255.0 alpha:1.0];
//set the unclicked layer
_unClickedButtonBorder.backgroundColor = [UIColor colorWithRed:86/255.0 green:88/255.0 blue:87/255.0 alpha:1.0];
}
And this is adding the views and changing the text colour according to the state of the button
-(void) setButtonsVisuals:(UIButton*)clickedButton {
[_clickedButtonBorder removeFromSuperview];
[_unClickedButtonBorder removeFromSuperview];
for (UIButton* button in _albumsTypesButtons)
{
if (button == clickedButton)
{
button.titleLabel.textColor = [UIColor colorWithRed:225/255.0 green:112/255.0 blue:119/255.0 alpha:1.0];
[button addSubview:_clickedButtonBorder];
}
else
{
button.titleLabel.textColor = [UIColor colorWithRed:199/255.0 green:200/255.0 blue:196/255.0 alpha:1.0];
[button addSubview:_unClickedButtonBorder];
}
}
}
My issue:
When adding the views as sub views to the button, it doesn't change
the text colour...
I think it might be something with the layers or something like that, but it really don't know
The solution was actually to use the [button setTitleColor] instead of touching it's properties.

How to fix offset keyboard when UISearchbar is touched

The problem I'm having is that when a user touches into the search bar the keyboard appears fine. Then when the presentQR method (see below) is called and then dismissed and then the searchbar is touched the keyboard appears as shown in the screenshot where it is offset. This is iOS7; not sure if that matters though.
I'm adding a UISearchbar using the following code:
// search bar
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(90.0, 0.0, 200.0, 44.0)];
searchBarView.autoresizingMask = 0;
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 190, 40)];
searchBar.backgroundImage = [[UIImage alloc] init];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.navigationItem.titleView = searchBar;
searchBar.delegate = self;
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,[UIColor whiteColor],UITextAttributeTextShadowColor,[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],UITextAttributeTextShadowOffset, nil] forState:UIControlStateNormal];
UITextField *textfield = [[UITextField alloc] init];
searchBar.placeholder = #"City Search";
// hide magnifying glass
textfield = nil;
for (UIView* subView in searchBar.subviews) {
if ([subView isKindOfClass:[UITextField class]]) {
textfield = (UITextField*)subView;
[(UITextField*)subView setTextColor:[UIColor whiteColor]];
[(UITextField*)subView setFont:[UIFont fontWithName:#"DIN-Regular" size:18]];
textfield.leftViewMode = UITextFieldViewModeNever;
textfield.rightViewMode = UITextFieldViewModeAlways;
break;
}
}
UIButton *cancelButton;
for (id button in searchBar.subviews)
{
if ([button isKindOfClass:[UIButton class]])
{
cancelButton=(UIButton*)button;
break;
}
}
[searchBar addSubview:textfield];
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;
-(void)presentQR {
QRReaderViewController *qr = [[QRReaderViewController alloc]initWithNibName:#"QRReaderViewController" bundle:nil];
UIButton *removeQRBtn=[UIButton buttonWithType:UIButtonTypeCustom];
removeQRBtn.frame=CGRectMake(260, 10, 60, 40);
[removeQRBtn addTarget:self action:#selector(removeQR) forControlEvents:UIControlEventTouchDown];
[removeQRBtn setImage:[UIImage imageNamed:#"qrcode.png"] forState:0];
[qr.view addSubview:removeQRBtn];
qr.view.backgroundColor = customColorGrey;
qr.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;
// [self presentModalViewController:qr animated:YES];
[self presentViewController:qr animated:YES completion:nil];
}
-(void)removeQR {
// [self dismissModalViewControllerAnimated:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}
1st Make sure that in QRReaderViewController there is nothing like reframing window or such. There might be an element conflicting with UIWindow preventing keyboard to be shown correctly. (e.g unbalanced orientation changes.)
2nd If you didn't find anything there you could try accessing the keyboard view using the following code and attempt to reset the frame. You could put this code in keyboardWillShow method (need to register for the notification)
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html#//apple_ref/c/data/UIKeyboardWillShowNotification
PS. I would suggest to just log the keyboard frame so you know when it's changed.
Reframing Keyboard: Found here: https://stackoverflow.com/a/10957843/1017340
//The UIWindow that contains the keyboard view - It some situations it will be better to actually
//You need to iterate through each window to figure out where the keyboard is, but In my applications case
//I know that the second window has the keyboard so I just reference it directly
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
//Because we cant get access to the UIPeripheral throughout the SDK we will just use UIView.
//UIPeripheral is a subclass of UIView anyways
UIView* keyboard;
//Iterate though each view inside of the selected Window
for(int i = 0; i < [tempWindow.subviews count]; i++)
{
//Get a reference of the current view
keyboard = [tempWindow.subviews objectAtIndex:i];
//Assuming this is for 4.0+, In 3.0 you would use "<UIKeyboard"
if([[keyboard description] hasPrefix:#"<UIPeripheral"] == YES)
{
NSLog(#"Keyboard Frame: %#",NSStringFromCGRect(keyboard.frame));
//HERE : try reframing the keyboard
//[keyboard setFrame:CGRectMake(...)]; /*NOT TESTED NOT RECOMMENDED*/
}
}

How to set Uilabel text to nil +iphone

I have dynamic UIButtons with subview is UIlabel.
My requirement is to display one label text in one button first time, after based on user selection(long press on the button) need to update the particular button subview label text and remove rest of the button subview label text.
I tried in this way
for (int i=0; i < [arr count]; i++)
{
UILabel *myLbl = [[UILabel alloc] initWithFrame:CGRectMake(30, 70, 60, 21)];
if (myissue.tag ==1) {
myLbl.text = #"Default";
}else {
myLbl.text = #"";
}
myLbl.backgroundColor = [UIColor clearColor];
myLbl.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
[myLbl setFont:[UIFont fontWithName:#"Helvetica" size:12]];
myLbl.textAlignment = NSTextAlignmentCenter;
myLbl.tag = i+1;
[myButton addSubview:defaultLbl];
[myLbl release];
}
And for retrieving the UILabel text
- (void)longPressTap:(UILongPressGestureRecognizer *)sender
{
if ([recognizer.view tag]) {
for (UIButton *btn in scrollView.subviews) {
UIButton *btnTag = (UIButton *)btn;
NSLog(#"--sv:%#", btn.subviews);
if (recognizer.view.tag == btnTag.tag){
[[btn.subviews objectAtIndex:3] text] ;
}else {
[[btn.subviews objectAtIndex:3] textAttributesForNil] ;
}
}
}
}
}
my problem is i was not able to selected button subview label text and removing rest of the button label text.
please help me.
You shouldnt need to add a label as a subview for a button, buttons already have their own text label that can be set using [button setTitle:(NSString *) forState:(UIControlState)]
and to remove the text just set seTitle:#""

Resources