Remove UiView removeFromSuperview with button action - uiview

I'm having problems with the action of a button ... I do not understand why things are not working and wanted to ask you if you could help me understand the error.
In my viewController I have a button in the selected mode adds a UIView inside another UIView but when the button is brought in was not selected should eliminate the view previously entered, it remains there without doing anything ... I tried in this way, but I do not understand where I'm wrong
- (IBAction)shareActive:(id)sender {
UIView *checkActive = [[UIView alloc]init];
checkActive.frame =CGRectMake(2, 2, 11, 11);
checkActive.layer.masksToBounds = YES;
checkActive.layer.cornerRadius = 5.5f;
checkActive.backgroundColor = [UIColor greenColor];
if (!self.condividiButton.selected) {
self.condividiButton.selected = YES;
[self.checkCondividi addSubview:checkActive];
NSLog(#"attivo");
}else {
self.condividiButton.selected = NO;
[checkActive removeFromSuperview];
NSLog(#"disattivo");
}
}

here is one of the many possible solutions.
I'm sure the others will give you different ideas about how such problem can be also solved.
- (IBAction)buttonShareTouchedUpInside:(UIButton *)sender {
if (!self.condividiButton.selected) {
UIView *checkActive = [[UIView alloc]init];
checkActive.tag = 121212;
checkActive.frame =CGRectMake(2, 2, 11, 11);
checkActive.layer.masksToBounds = YES;
checkActive.layer.cornerRadius = 5.5f;
checkActive.backgroundColor = [UIColor greenColor];
self.condividiButton.selected = YES;
[self.checkCondividi addSubview:checkActive];
} else {
self.condividiButton.selected = NO;
[self.checkCondividi.subviews enumerateObjectsUsingBlock:^(UIView * obj, NSUInteger idx, BOOL *stop) {
if (obj.tag == 121212) {
[obj removeFromSuperview];
}
}];
}
}
NOTE: the original problem can be resolved via many other and probably more elegant ways, but I'm not concerning about those here.

Related

How can I add spacing between navigation bar and UISearchBar?

I would like to add a progress bar between navigation bar and UISearchBar. May I know how can I implement this? Please help. Thank you.
Here is my current code in Cell.m
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect barFrame = CGRectInset(self.searchBar.frame, 10.0f, 10.0f);
self.searchBar.frame = barFrame;
}
Here is my current code in ViewController.m
Did not reflect in this code after edited. _searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 24)];
- (void)viewDidLoad {
[super viewDidLoad];
_valueProgress = [[LDProgressView alloc] initWithFrame:CGRectMake(0,DCNaviH, ScreenW, 10.0f)];
_valueProgress.type = LDProgressSolid;
_valueProgress.color = ThemeRedColor;
_valueProgress.progress = 0.40;
_valueProgress.flat = #YES;
_valueProgress.showText = #NO;
[self.view addSubview:_valueProgress];
}
- (UISearchBar *)searchBar{
if (!_searchBar) {
_searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 24)];
[_searchBar setBackgroundImage:[UIImage imageNamed:#"ic_searchBar_bgImage"]];
}];
}
return _searchBar;
}
There are a few simple options for you depending on your apps UX. I think the best solution for you based on how you explained your issue would be to include the progress bar to your view and make sure it's above the other views while positioning it below the navigation bar.
_valueProgress = [[LDProgressView alloc] init];
_valueProgress.translatesAutoresizingMaskIntoConstraints = NO;
_valueProgress.type = LDProgressSolid;
_valueProgress.color = ThemeRedColor;
_valueProgress.progress = 0.40;
_valueProgress.flat = #YES;
_valueProgress.showText = #NO;
[self.view addSubview:_valueProgress];
[_valueProgress.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].isActive = YES;
[_valueProgress.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].isActive = YES;
[_valueProgress.heightAnchor constraintEqualToConstant:10.0f].isActive = YES;
if (#available(iOS 11.0, *)) {
[_valueProgress.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor].isActive = YES;
} else {
[_valueProgress.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor].isActive = YES;
}
And if the search bar has been added above the progress bar, you can always call [self.view bringSubviewToFront:_valueProgress] afterwards.

UIPIckerView showing strange behaviour

I'm new to iOS development and I have an assignment where I need to implement a a UITextfield which upon a tap, brings a country selector UIPickerView.
I went through some online tutorials and got it working somehow. But it shows some strange behaviour which I can't seem to figure out.
Some strange black bars are appearing, with the picker content visibly duplicated each time I scroll.
I believe I've made a mistake in my code, forgive my lack of knowledge but can't seem to figure out what is wrong in the code.
Could you please tell me what has gone wrong?
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField isEqual:self.countryTextField]) {
countryPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
countryPicker.showsSelectionIndicator = YES;
self.countryPicker.delegate = self;
self.countryPicker.dataSource = self;
// [self.countryPicker reloadAllComponents];
countryPicker.showsSelectionIndicator = YES;
// [countryPicker init:self];
textField.inputView = countryPicker;
}
return YES;
}
- (UIView * ) pickerView: (UIPickerView * ) pickerView viewForRow: (NSInteger) row forComponent: (NSInteger) component reusingView: (UIView * ) view {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)];
label.text = [NSString stringWithFormat:#"%#", [[self.countries objectAtIndex:row] objectForKey:#"name"]];
[label setTextAlignment:UITextAlignmentLeft];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont boldSystemFontOfSize:15]];
return label;
}
Might be initialising every time , instead of initialising at every time , Check whether the UIPicker is already initialised
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField isEqual:self.countryTextField]) {
if(! countryPicker){
countryPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
countryPicker.showsSelectionIndicator = YES;
self.countryPicker.delegate = self;
self.countryPicker.dataSource = self;
countryPicker.showsSelectionIndicator = YES;
}
textField.inputView = countryPicker;
}
return YES;
}
It seems there are few things need to be changed in your code
1.Don't allocate memory to UIPickerView every time your text field get edited -textFieldShouldBeginEditing
if(self.countryPicker == nil)
{
self.countryPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
self.countryPicker.showsSelectionIndicator = YES;
self.countryPicker.delegate = self;
self.countryPicker.dataSource = self;
self.countryPicker.showsSelectionIndicator = YES;
}
2.Either use self.countrypicker -> class property or countrypicker don't use them interchangeably.
Well I finally found the issue. It was a silly mistake of defining a wrong number of components in numberOfComponentsInPickerView

Reverse Swipe Through Array of UIButtons

In my app, I have a lot of UIButtons stack on top of each other. I want to use Swipe Gestures to go back and forth through them. in viewDidLoad I have:
self.buttonArray = [[NSMutableArray alloc] initWithObjects:map, boardmembers, facebook, twitter, lessonbooks, schedule, nil];
map.hidden = NO;
boardmembers.hidden = facebook.hidden = twitter.hidden = lessonbooks.hidden = schedule.hidden = YES;
To swipe from right to left, and advance them I have:
UIButton *currentVisibleButton = [_buttonArray firstObject];
UIButton *nextVisibleButton = [_buttonArray objectAtIndex:1];
[_buttonArray removeObject:currentVisibleButton];
[_buttonArray addObject:currentVisibleButton];
currentVisibleButton.hidden = YES;
nextVisibleButton.hidden = NO;
I am having issues getting the reverse to work, where I can go back AND forth. How would I do this?
I'm not sure I understand your code exactly, but the analogous reverse order of what you posted would be:
UIButton *currentVisibleButton = [_buttonArray lastObject];
UIButton *nextVisibleButton = _buttonArray[_buttonArray.count-2];
[_buttonArray removeObject:currentVisibleButton];
[_buttonArray insertObject:currentVisibleButton atIndex:0];
But a better way to use an array as a ring is to keep a cursor. Keep state of the currently visible index.
#property(assign, nonatomic) NSInteger cursor;
- (void)cursorLeft {
self.cursor = (self.cursor+1 == _buttonArray.length)? 0 : self.cursor+1;
}
- (void)cursorRight {
self.cursor = (self.cursor == 0)? _buttonArray.length-1 : self.cursor-1;
}
- (UIView *)viewAtCursor {
return (UIView *)_buttonArray[self.cursor];
}
Make sure button at index zero in the array is visible and all the others are hidden. Now without doing any churning on the array, you can swipe around like this.
// swipe left
self.viewAtCursor.hidden = YES;
[self cursorLeft];
self.viewAtCursor.hidden = NO;
// swipe right
self.viewAtCursor.hidden = YES;
[self cursorRight];
self.viewAtCursor.hidden = NO;
Try this,
- (void)swipeToLeft:(BOOL) moveLeft {
UIButton *currentVisibleButton = [_buttonArray firstObject];
UIButton *nextVisibleButton;
if (moveLeft) {
nextVisibleButton = [_buttonArray objectAtIndex:1];
} else {
nextVisibleButton = [_buttonArray lastObject];
}
currentVisibleButton.hidden = YES;
nextVisibleButton.hidden = NO;
[_buttonArray removeObject:currentVisibleButton];
[_buttonArray addObject:currentVisibleButton];
}

Issue in deleting the textfield

I have two buttons for adding and deleting the textfields. Adding works fine but when I click on delete it deletes only the last added textfield. Here are my two methods:
-(void)addTextField {
keyTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, yAxisDistance, 150, 30)];
keyTextField.borderStyle = UITextBorderStyleRoundedRect;
keyTextField.placeholder = #"Key Value";
keyTextField.delegate = self;
[self.view addSubview:keyTextField];
valueTextField = [[UITextField alloc] initWithFrame:CGRectMake(165, yAxisDistance, 150, 30)];
valueTextField.borderStyle = UITextBorderStyleRoundedRect;
valueTextField.placeholder = #"Value";
valueTextField.delegate = self;
[self.view addSubview:valueTextField];
yAxisDistance = yAxisDistance+35;
}
-(void)deleteTextField {
[keyTextField removeFromSuperview];
[valueTextField removeFromSuperview];
yAxisDistance = yAxisDistance-35;
}
I know it's an small issue but I am very new to this field so kindly help.
Use this code for remove specific textfield from UIView. But First you have to set tag of every UITextField in view when you create or add it in view.
for ( UITextField* textField in view.subviews )
{
if(textField.tag== 1)
{
[textField removeFromSuperview];
}
}
I think the problem lies with the outlet. Can you check the following:
Open the outlets that are connected to the textifeld in the IB. There should be gray dots inside circles on the left side of the editor. Are they seem correct for both text fields?
Setup a breakpoint inside deleteTextField method and check the two textfields. Verify that both properties are not nil.
PS: You don't need to add tags to your view, using properties is perfectly fine and even better in my opinion. The reason for your problem is something else. Also, you do not need to removeFromSuperview, you can also setHidden:YES.
Sorry that time I was not understand your problem...
I done with this:
Declare one
NSMutableArray *allTextfieldArray;
and initialise in
viewdidload
method..
now do:
-(void)addTextField {
keyTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, yAxisDistance, 150, 30)];
keyTextField.borderStyle = UITextBorderStyleRoundedRect;
keyTextField.placeholder = #"Key Value";
keyTextField.delegate = self;
[self.view addSubview:keyTextField];
valueTextField = [[UITextField alloc] initWithFrame:CGRectMake(165, yAxisDistance, 150, 30)];
valueTextField.borderStyle = UITextBorderStyleRoundedRect;
valueTextField.placeholder = #"Value";
valueTextField.delegate = self;
[self.view addSubview:valueTextField];
yAxisDistance = yAxisDistance+35;
[allTextfieldArray addObject:keyTextField];
[allTextfieldArray addObject:valueTextField];
}
if ([allTextfieldArray count]>0) {
UITextField *txtField = [allTextfieldArray lastObject];
[allTextfieldArray removeLastObject];
[txtField removeFromSuperview];
txtField = nil;
UITextField *txtField2 = [allTextfieldArray lastObject];
[allTextfieldArray removeLastObject];
[txtField2 removeFromSuperview];
txtField2 = nil;
yAxisDistance = yAxisDistance-35;
}
you store the last textField in your variables keyTextField and valueTextField. So when you call your deleteTextField the last both will be deleted. you must track which textFileds you want exactly to delete.
for example you could give all your textFields a tag number when you create them:
first create an int as counter:
#implementation YourClass {
int tagcounter;
}
in init method set your counter:
tagcounter = 0;
in your addTextField:
keyTextField.tag = tagcounter++;
valueTextField.tag = tagcounter++;
when the delete button is tapped, you must know the textfield tags and pass them to your deleteTextField method. There you could do something like:
-(void)deleteTextFieldWithTag:(int)tagnumber {
for (UIView *view in [self.view subviews])
{
if ([view isKindOfClass:[UITextField class]])
{
if (view.tag == tagnumber || view.tag == tagnumber+1) {
[view removeFromSuperview];
}
}
}

Determine if current screen has visible navigation bar

I have a singlton object. Is there any simple way to determine if current screen contains a navigation bar within singlton methods?
The singleton is UIView subclass. It's designed for showing prorgess activity, e.g. network exchange. It looks like black rectangle dropping down from top and hiding when the work is done. Why singleton? It's easy to call it from any place of code
The followed snippet is showing the initialization of activity singleton and published here just for better understaning my idea.
-(void) showUpdatingView:(NSString *) msg {
[self initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
activity = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
activity.frame = CGRectMake(5, 10, 22, 22);
labelView = [[[UILabel alloc] initWithFrame:CGRectMake(35, 10, [UIScreen mainScreen].bounds.size.width - 10, 22)] autorelease];
labelView.font = [UIFont boldSystemFontOfSize:12];
labelView.backgroundColor = [UIColor clearColor];
labelView.textColor = [UIColor whiteColor];
labelView.text = msg;
[self addSubview:activity];
[self addSubview:labelView];
self.backgroundColor = [UIColor blackColor];
self.alpha = 0.7;
}
The activity can be called by
[[ActivitySingleton getInstance] showUpdatingView:#"Getting data."];
it's not all.
The singleton is being created in AppDelegate object and the view is added to
inlineActivity = [[CHInlineActivityView alloc] initView];
[self.window.rootViewController.view addSubview:inlineActivity];
I know it may look crazy. But when I was designing it seemed to me reasonable
if you have all in one navigationController:
BOOL navHidden = self.window.rootViewController.navigationController.navigatonBarHidden;
if you don't it is a bit harder.. you could check the window's subviews and see if you can find a UINavigationBar
id navbar = [self.window firstSubviewOfKind:[UINavigationBar class] withTag:NSNotFound];
BOOL navHidden = navbar == nil;
#implementation NSView (findSubview)
- (NSArray *)findSubviewsOfKind:(Class)kind withTag:(NSInteger)tag inView:(NSView*)v {
NSMutableArray *array = [NSMutableArray array];
if(kind==nil || [v isKindOfClass:kind]) {
if(tag==NSNotFound || v.tag==tag) {
[array addObject:v];
}
}
for (id subview in v.subviews) {
NSArray *vChild = [self findSubviewsOfKind:kind withTag:tag inView:subview];
[array addObjectsFromArray:vChild];
}
return array;
}
#pragma mark -
- (NSView *)firstSubviewOfKind:(Class)kind withTag:(NSInteger)tag {
NSArray *subviews = [self findSubviewsOfKind:kind withTag:tag inView:self];
return subviews.count ? subviews[0] : nil;
}
#end

Resources