[UIRoundedRectButton setText:]: unrecognized selector sent to instance? - ios

My program kept terminating due to an uncaught exception. The keyboard works fine until a character is entered on the keyboard.
I have a button to bring up the keyboard and an outlet hooked up to a text field, I'm also the text field delegate.
[code removed]
Thank you in advance for any help :)

This line lbl.text=TempTF.text; is not good in your case. Your lbl can be any type of UIView since you just typecast it from subviews. So in your case one of them is a type of UIRoundedRectButton which has no setter for text.
You need to put some if([lbl isKindOfClass:]) or do a check if([lbl respondsToSelector:#selector(setText:)])

-(void)changeLabelsMethod:(UITextField*)txtField
{
NSArray *allLabelViews = self.view.subviews;
for(id subView in allLabelViews) {
if([allLabelViews isKindOfClass:[LettersOnTileView class]])
{
for (int i = 0; allLabelViews.count>0; i++) {
LettersOnTileView.tag = i;
if([[allLabelViews objectAtIndex:i] isKindOfClass:[UILabel Class]]){
UILabel *lbl=[allLabelViews objectAtIndex:i];
lbl.text=TempTF.text;
}
}
}
}

You have issue in your code.
Instead of this code.
for(id subView in allLabelViews) {
if([allLabelViews isKindOfClass:[LettersOnTileView class]])
{
You have to try this.
for(id subView in allLabelViews) {
if([subView isKindOfClass:[LettersOnTileView class]])//Here you want to check all subview .
{
2) I think you have got button object in this line UILabel *lbl=[allLabelViews objectAtIndex:i];. That's y it get crashed logged in next line.

Related

Test if object is an instance of class UISegment

So I'm doing some ugly hacks to modify the appearance of individual segments in a UISegmentedControl. When I walk down the UISegmentedControl's subview array and print each immediate subview's class name, I can see there are instances of UISegment for each segment in the control:
for (UIView *view in [[self segments] subviews])
{
NSLog(#"%#", NSStringFromClass([view class]));
}
Now apparently Xcode can't resolve this class, so I can't use the familiar [view isKindOfClass:[UISegment class]].
I assumed I was just missing a header to include, but I also can't find any docs for it on Apple's developer reference site.
So how would I go about testing if an object is an instance of UISegment? And what exactly is this class - whose name can be resolved at run-time but not at compile-time?
edit
What I'm trying to achieve is a clear indication that a particular segment is enabled or disabled. For instance, when I disable an entire UISegmentedControl, the following works great to update both selected and unselected segment colors/fonts:
- (void)setIsEnabled:(BOOL)isEnabled
{
[super setIsEnabled:isEnabled];
// be careful and ensure you use the isEnabled getter because it takes isEnabledForConfiguration into account
[[self segments] setUserInteractionEnabled:[self isEnabled]];
[[self segments] setTintColor:[self interactionColor]];
[[self segments] setTitleTextAttributes:[NSDictionary dictionaryWithObject:[self enabledTextColor]
forKey:NSForegroundColorAttributeName]
forState:UIControlStateNormal];
}
But sometimes only a subset of the segments should be disabled, and I would like to visually make that apparent (the -(void)setEnabled:forSegmentAtIndex: method doesn't seem to have any visible effect).
So I've found that I can modify these UISegment subviews to achieve the visual updates (nasty hack, isn't guaranteed to work in the future, I know...).
To try protecting against those future API changes, I've done my best to implement a flexible way to grab a reference to the UISegment objects by segment index.
First, I grab the title of the segment at the input segment index, then traverse all the way down the UISegmentedControl view hierarchy until I find something that responds to the text selector and whose text value equals the title. That view always seems to be a UISegmentLabel (which is another private class!), and his superview is the UISegment I'm looking for. So I've come up with:
- (UIView *)getSegmentAtIndex:(NSInteger)index
{
NSString *title = [[self segments] titleForSegmentAtIndex:index];
id segment = nil;
if (nil != title && 0 < [[EFBGlobalUtil trimWhitespace:title] length])
{
segment = [[EFBGlobalUtil viewWithCondition:^BOOL(UIView *view)
{
return [view respondsToSelector:#selector(text)] && [title isEqualToString:[view valueForKey:NSStringize(text)]];
}
inView:[self segments]] /* this dude -> */superview];
}
return segment;
}
The question really stems from that superview getter I stuck on the end. I would like to verify the superview is actually a UISegment, but the solution to that will also help me check for UISegmentLabel objects as well (instead of the text selector).
And for reference, here's the +(void)viewWithCondition:inView: utility routine:
+ (UIView *)viewWithCondition:(BOOL (^)(UIView *view))condition inView:(UIView *)view
{
// non-recursive BFS traversal of a view hierarchy until specified UIView matching condition is found
NSMutableArray<UIView *> *stack = [[NSMutableArray alloc] initWithObjects:view, nil];
UIView *curr;
while ([stack count] > 0)
{
curr = [stack firstObject];
[stack removeObject:curr];
if (condition((UIView *)curr))
{
return (UIView *)curr;
}
[stack addObjectsFromArray:[curr subviews]];
}
return nil;
}

Expected Identifier

Please can anyone tell me why this piece of code isn't working? I've got a dictionary which contains UIViews with tables inside associated with the keys which are the names of the corresponding buttons (there are a lot of them). So what I actually want to do is to change the view visibility on the corresponding button click. But the issue is that the expression to do that is not accepted by Xcode and I get the Expected Identifier error.
- (IBAction)choosingButtonClicked:(id)sender {
if ([sender currentTitle]) {
[(UIView *)[self.selectionTables objectForKey:[sender currentTitle]]].hidden = ![(UIView *)[self.selectionTables objectForKey:[sender currentTitle]]].isHidden;
}
}
First of all, with all due respect, I agree with trojanfoe comments. Its not working because its not properly written.
Now, lets try to streamline it with below code:
- (IBAction)choosingButtonClicked:(id)sender {
NSString *title = [sender currentTitle];
if (title) {
UIView *selectionView = (UIView *)self.selectionTables[title];
selectionView.hidden = !selectionView.isHidden;
}
}
Your code is too complex, because of that even the author can't understand it. If we re-write your code using local variables, it will look like:
- (IBAction)choosingButtonClicked:(id)sender
{
NSString *title = [sender currentTitle];
if (title)
{
UIView *tempView = (UIView *)[self.selectionTables objectForKey:title];
[tempView].hidden = ![tempView].isHidden;
}
}
If you check the code now, you can see that the following code is causing the issues:
[tempView].hidden = ![tempView].isHidden;
Change your method like:
- (IBAction)choosingButtonClicked:(id)sender
{
NSString *title = [sender currentTitle];
if (title)
{
UIView *tempView = (UIView *)[self.selectionTables objectForKey:title];
tempView.hidden = !(tempView.isHidden);
}
}

In iOS ARC my recursive function crashes application with EXC_BAD_ACCESS

Following code of mine generates crash in ARC mode:
MxTextField.m
+enableAllTextFields:(BOOL)enable InViews:(__weak UIView*) view
{
#try
{
NSArray* textFields = view.subViews;
for(int idx = 0; idx < textFields.count; idx++)
{
__weak UIView* view = [textFields objectAtIndex:idx];
if(view.subViews.count > 0)
[MxTextField enableAllTextFields:enable InView:view];
else
NSLog(#"No SubViews");
if([view class] == [MxTextField class])
[(MxTextField*) view setEnabled:enable];
}
}
#catch(NSException exception)
{
NSLog(#"%s : %#",__func__,exception);
}
}
After Some Loop on the execution of this function It crashes by showing breakpoint at the end of the function saying EXC_BAD_ACCESS. Can anyone help me out that what goes wrong in this implementation?
Any help will be thankful.
Putting aside many other problems the only reason for a crash that I can see from the posted code is that your method is supposed to return an object but does not do so.
Explanation: While it's not common to leave out the return type in Objective-C it's perfectly legal. It means that the method returns an object of type id.
Since your method lacks a return statement the returned value is undefined. This confuses ARC and probably makes it autorelease the random value in the return register which, eventually, leads to the crash.
Here's a proper version of your method:
+ (void)forAllTextFieldsIn:(UIView *)view setEnabled:(BOOL)enabled
{
if ([view isKindOfClass:[MxTextField class]])
[(MxTextField *)view setEnabled:enabled];
for (UIView *subview in view.subviews)
[self forAllTextFieldsIn:subview setEnabled:enabled];
}
The problem could be the method adopted for iteration and also try-catch is not a good practice, use fast-enumeration for faster and reliable result . The below code could resolve your problem
+(void)enableAllTextField:(BOOL)enable inView:(UIView *)contrainerView
{
for (UIView *subview in contrainerView.subviews) {
if(subview.subviews.count>0)
[MxTextField enableAllTextField:enable inView:subview];
else if ([subview isKindOfClass:[MxTextField class]]) {
MxTextField *textField = (MxTextField *)subview;
[textField setEnabled:enable];
}
}
}

How to get text from textfield tag in iPhone

I am creating text fields dynamically and each text field has tag.Now, I am trying to get text of textfield using that tags but, it returning UIButton and my application getting crashed.How can i get text from tags? please help me.
Here is my code to get text :
UITextField *textValue = (UITextField *)[self.view viewWithTag:textField.tag];
NSString *chkText = textValue.text;
NSLog(#"entered Text %#",chkText);
my application is crashing at this line :
NSString *chkText = textValue.text;
and error is :
-[UIButton text]: unrecognized selector sent to instance
The main problem is your view contains button as well. So when you are trying to access it. It is returning UIButton object. So it is always better that check the condition whether it is button type or textfield type object. Try below:-
id nextTextField = [self.view viewWithTag:textField.tag];
nextTextField=([nextTextField isKindOfClass:[UITextField class]]) ? nextTextField.text : nextTextField.title;
NSLog(#"%#", nextTextField);
From your code (viewWithTag:textField.tag), If you can access your textField then why are you accessing it once more? However, the below description will help you
You have assigned the same tag to one of your UIButton control in the same view. Also make sure that textField.tag is greater than Zero.
The below code will fix the crash, however, it will not resolve your issue.
UITextField *textFeild = [[self.view viewWithTag:textFieldTag] isKindOfClass:[UITextField class]]?(UITextField *)[self.view viewWithTag:textFieldTag]:nil;
if(nil != textFeild)
{
NSString *chkText = textFeild.text;
NSLog(#"entered Text %#",chkText);
}
else
{
NSLog(#"textFeild is null");
}
Set textfield tag as any unique value like "346". (Avoid using 0 as tag value). And Use this --
UITextField *textValue = nil;
id myObj = [self.view viewWithTag:yourTagValue];
if([myObj isKindOfClass:[UITextField class]])
{
textValue = (UITextField*)myObj;
NSString *chkText = textValue.text;
NSLog(#"entered Text %#",chkText);
}
When you use tag, using NS_ENUM is a good habit.
NS_ENUM(NSUInteger, TEST_TAGS)
{
TAG_TEXT_INPUT,
TAG_BTN_GO,
};
Then you can use it.
[textField setTag:TAG_TEXT_INPUT];
First check to which UIVIew you are adding this textfield as a subview.
Take that UIVIew instance and call viewWithTag method then your code works.

Unrecognized selector sent to an instance?

I have three methods which are taking parameters,
I am taking exception at this parameter giving,
[QuestionnaireView continueSingle:withQuestion:question:]: unrecognized selector sent to instance 0x8a4b1c0
What am i doing wrong? Its definition is also given in the header file.
Here is my code;
-(void) continueSingle:(id)sender withQuestion:(Question*)quest{
int counter = 0;
NSString * tempAnswer;
for(UIView* subview in [sender superview].subviews)
{
if([subview isKindOfClass:[UIButton class]])
{
if([((UIButton*)subview) isSelected])
{
counter++;
tempAnswer = [NSString stringWithFormat:#"%#",((UIButton*)subview).currentTitle];
}
}
}
}
Your mistake is here
-(void) continueSingle:(id)sender withQuestion:(Question*)quest
Because you are passing three parameter but you're receiving only two parameter. So you need to take 3 parameter. Like this..
-(void) continueSingle:(id)sender withQuestion:(Question*)quest question:(Question *)question1

Resources