I am developing an application. In that I get all subviews of UITableViewCell.
The code for this one is:
(void)listSubviewsOfView:(UIView *)view {
// Get the subviews of the view
NSArray *subviews = [view subviews];
// Return if there are no subviews
if ([subviews count] == 0) return;
for (UIView *subview in subviews) {
NSLog(#"%#", subview);
// List the subviews of subview
[self listSubviewsOfView:subview];
}
}
But my problem is how to find out button from that subviews list. Please tell me how to solve this one.
You can iterate through all subviews like this.
for (id subview in subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
//do your code
}
}
Swift
for subview in view.subviews {
if subview is UIButton {
// this is a button
}
}
Check if the subview is a button with is AnyClass, isKind(of aClass: AnyClass) or isMember(of aClass: AnyClass) API.
Using is-
for subview in self.view.subviews{
if subview is UIButton{
//do whatever you want
}
}
Using isMember(of:)-
for subview in self.view.subviews{
if subview.isMember(of: UIButton.self){
//do whatever you want
}
}
Using isKind(of:)-
for subview in self.view.subviews{
if subview.isKind(of: UIButton.self){
//do whatever you want
}
}
To expand on the 'do your code' part of Martin's answer. Once I got the subview, I wanted to test whether it was the right button and then remove it. I can't check the titleLabel of the button directly by using subview.titleLabel so I assigned the subview to a UIButton and then checked the button's titleLabel.
- (void)removeCheckboxes {
for ( id subview in self.parentView.subviews ) {
if ( [subview isKindOfClass:[UIButton class]] ) {
UIButton *checkboxButton = subview;
if ( [checkboxButton.titleLabel.text isEqualToString:#"checkBoxR1C1"] ) [subview removeFromSuperview];
}
}
}
Related
I have used this piece of code to set Text Color for the Texfield Inside the UISearchBar.
for (UIView* view in subViews)
{
if([view isKindOfClass:[UITextField class]])
{
UITextField* searchTextField = (UITextField*)view;
[self setTextColorToTextField:searchTextField];
break;
}
else
{
for (id view1 in view.subviews)
{
if ([view1 isKindOfClass:[UITextField class]])
{
UITextField* searchTextField = (UITextField*)view1;
[self setTextColorToTextField:searchTextField];
break;
}
}
}
}
Can anyone suggest how to improve the code?
Well there is no need to code when you can set its colour in the StoryBoard.
Here add this line in identity inspector of searchBar.
I am using the following to try to access all the UIButtons located inside of a UIScrollView within a UIView. The problem is that the code doesn't seem to locate the buttons and set the border property.
UIView -> UIScrollView -> UIButtons.
I basically want to loop through the buttons and set the border property.
for(UIView *v in [self.viewLightLeakChoices subviews]) {
if([v isKindOfClass:[UIButton class]]) {
v.layer.borderWidth = 0;
}
}
Try this one instead
for (id obj in scrollView.subviews) {
NSString *classStr = NSStringFromClass([obj class]);
if ([classStr isEqualToString:#"UIButton"]) {
UIButton *button = (UIButton*)obj;
button.layer.borderWidth = 2.0;
button.layer.borderColor = [UIColor greenColor].CGColor;
}
}
output
Thanks for the help guys. I ended up doing a bunch of for loops to get down to the UIButtons.
for(UIView *v in [self.viewLightLeakChoices subviews]) {
if([v isKindOfClass:[UIScrollView class]]) {
for(UIView *subView in [v subviews]) {
for(UIButton *btn in [subView subviews]) {
btn.layer.borderWidth = 0;
}
}
}
}
first make sure you are getting subviews of UIScrollView because your structure is
UIView > UIScrollView> UIButton
if you have only 1 scroll view in self.viewLightLeakChoices then set your scrollview tag = 1000 and direct access your scrollview so now you dont need to use loop. and execution will be fast.
UIScrollView *scrlV = [self.viewLightLeakChoices viewWithTag:1000];
for (UIButton *btn in scrlV.subviews)
{
if ([btn isKindOfClass:[UIButton Class]]) {
btn.layer.borderWidth = 1.0;
btn.layer.borderColor = [UIColor whiteColor].CGColor;
}
}
i am unable to get i am expecting you are having scrollview -> uiview ->button
for(UIView *myview in Scrollview.subviews)
{
for ( id mybutton in myview.subviews)
{
if ([mybutton isKindOfClass:[UIButton class]])
{
UIButton *mybtn=(UIButton *)mybutton;
mybtn.layer.borderWidth=0;
}
}
}
You should have create Button class say MyButton extents UIButton, if you do this there is no need to loop through scrollview's subviews.
implement awakeFromNib method and apply border, to apply border refer this SO Post How to create border in UIButton?
I have a UIScrollview in a tab, i added some UIView (NIB) to the UIScrollview. Each UIView has some UISwictch, labels, buttons, etc. How can i get the the label.text inside of the view added to the UIScrollview.
I tried a lot of things but i can access to the content of the UIView added to the UIScrollview.
Check with this,
for (UIView *addedView in [self.scrollView subviews])
{
for (UIView *sub in [addedView subviews])
{
if([sub isKindOfClass:[UILabel class]])
{
UILabel *myLabel = (UILabel *)sub;
NSLog(#"My label :%#",myLabel .text);
}
}
}
Here scrollView is your scroll view. It will print all label's text.
If you need to print any particular label's text. Then add a tag to it and check the tag before printing it, like if(myLabel.tag == 7)
set a unique tag for your label.
eg: label.tag = 10345 (some random number, which is a unique tag)
and you can search for the label in the parentView like this
UILabel *theLabel = (UILabel *)[parentView viewWithTag: 10345];
and then you can do what ever you want with the Label.
Step 1: Add this code in your scrollview
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTap:)];
singleTapGestureRecognizer.numberOfTapsRequired = 1;
singleTapGestureRecognizer.enabled = YES;
singleTapGestureRecognizer.cancelsTouchesInView = NO;
[scrollView addGestureRecognizer:singleTapGestureRecognizer];
Step 2: Implement this method
-(void)singleTap:(UITapGestureRecognizer *)gesture
{
// Convert gesture into view for getting subviews
CGPoint point = [gesture locationInView:mainScrollView];
UIView *tappedView = [mainScrollView hitTest:point withEvent:nil];
// Get the subviews of the view
NSArray *subviews = [view tappedView];
// Return if there are no subviews
if ([subviews count] == 0) return;
for (UIView *subview in subviews) {
NSLog(#"%#", subview);
// List the subviews of subview
[self your_method:subview];
}
}
I have a UINavigationController. On the right top i have a button on click of which i have to get a drop down table view. I created another UIViewController Class, with xib and added it as a subView to the current view. It should appear on 1st click and disappear on the 2nd click. This should happen for all click(open view and close view). I wrote this code but dont know where i'm going wrong. someone please help
-(void)modalTableView
{
tableView1 = [[TableViewController alloc] initWithNibName:#"TableViewController" bundle:nil];
for (UIView *subView in self.view.subviews)
{
if ([subView isKindOfClass:[TableViewController class]])
{
[subView removeFromSuperview];
}
else
{
[self.view addSubview:tableView1.view];
}
}
}
What am i missing here?
EDIT : TableViewController is the name of my UIViewController Class
The clue is here
for (UIView *subView in self.view.subviews)
each subView is of class UIView and your test
isKindOfClass:[TableViewController class]
is testing for class TableViewController
I would suggest a way of doing this would be by tagging the views that you add dynamically, with say 99 - and then in your loop you can identify those views by their tag.
eg.
for (UIView *subView in self.view.subviews)
{
if (subView.tag == 99)
{
[subView removeFromSuperview];
}
}
Swift version
To remove a single subview:
subView.removeFromSuperview()
To remove all subviews:
for subView in self.subviews as [UIView] {
subView.removeFromSuperview()
}
Source: What is the best way to remove all views from parent view / super view?
Try this,
if ([subView isKindOfClass:[UITableView class]])
{
[subView removeFromSuperview];
}
Here is something that should go some way to working - assuming that tableView1 is a retained #property (If not then maybe this SO answer on lazy loading techniques is for you).
-(void)modalTableView
{
if (tableView1 != nil)
{
tableView1 = [[TableViewController alloc] initWithNibName:#"TableViewController" bundle:nil];
}
if (tableView1.view.superview == nil)
{
[self.view addSubview:tableView1.view];
} else
{
[tableView1.view removeFormSuperview];
}
}
I'm making an app where I add a subview to a view using addSubview: on an IBAction. In the same way, when the button with that IBAction is touched again should call removeFromSuperview on that subview added on that IBAction:
PSEUDO CODE
-(IBAction)showPopup:(id)sender
{
System_monitorAppDelegate *delegate = (System_monitorAppDelegate *)[[UIApplication sharedApplication] delegate];
UIView *rootView = delegate.window.rootViewController.view;
if([self popoverView] is not on rootView)
{
[rootView addSubview:[self popoverView]];
}
else
{
[[self popoverView] removeFromSuperview];
}
}
You are probably looking for UIView's -(BOOL)isDescendantOfView:(UIView *)view; taken in UIView class reference.
Return Value
YES if the receiver is an immediate or distant
subview of view or if view is the receiver itself; otherwise NO.
You will end up with a code like :
Objective-C
- (IBAction)showPopup:(id)sender {
if(![self.myView isDescendantOfView:self.view]) {
[self.view addSubview:self.myView];
} else {
[self.myView removeFromSuperview];
}
}
Swift 3
#IBAction func showPopup(sender: AnyObject) {
if !self.myView.isDescendant(of: self.view) {
self.view.addSubview(self.myView)
} else {
self.myView.removeFromSuperview()
}
}
Try this:
-(IBAction)showPopup:(id)sender
{
if (!myView.superview)
[self.view addSubview:myView];
else
[myView removeFromSuperview];
}
UIView *subview = ...;
if([self.view.subviews containsObject:subview]) {
...
}
The Swift equivalent will look something like this:
if(!myView.isDescendantOfView(self.view)) {
self.view.addSubview(myView)
} else {
myView.removeFromSuperview()
}
Check the superview of the subview...
-(IBAction)showPopup:(id)sender {
if([[self myView] superview] == self.view) {
[[self myView] removeFromSuperview];
} else {
[self.view addSubview:[self myView]];
}
}
Your if condition should go like
if (!([rootView subviews] containsObject:[self popoverView])) {
[rootView addSubview:[self popoverView]];
} else {
[[self popoverView] removeFromSuperview];
}
Here we used two different views. Parent view is the view in which we are searching for descendant view and check wether added to parent view or not.
if parentView.subviews.contains(descendantView) {
// descendant view added to the parent view.
}else{
// descendant view not added to the parent view.
}