Why is this code not updating my UITextField? - objective-c

if (![value integerValue]) {
textField.text = [[NSString alloc] initWithFormat:#"%d", *x];
[slider setValue:(float)*x];
}
if ([value integerValue]>100) {
textField.text = [[NSString alloc] initWithFormat:#"255"];
[slider setValue:100.0];
}
if ([value integerValue]<0) {
textField.text = [[NSString alloc] initWithFormat:#"0"];
[slider setValue:0.0];
}
[textField setText:value];
The textfield's value remains the one typed in, ignoring when I set it to a new value.
Can someone please explain why?

[textField setText:value];
sets the UITextField value back to value.

You are overwriting the textField.text property at the end of your method, no matter if it was "corrected" under one of your case-descisions.
Check that last line within your original code and note that textField.text = xyz is semantically equal to [textField setText:xyz].
To fix your issue, use this corrected version of your code:
if (![value integerValue]) {
textField.text = [[NSString alloc] initWithFormat:#"%d", *x];
[slider setValue:(float)*x];
}
else if ([value integerValue]>100) {
textField.text = [[NSString alloc] initWithFormat:#"255"];
[slider setValue:100.0];
}
else if ([value integerValue]<0) {
textField.text = [[NSString alloc] initWithFormat:#"0"];
[slider setValue:0.0];
}
else {
textField.text = value;
}

Related

Issue AVSpeechUtterance

I´m trying to make the AVSpeech speak up two NSString in the string *combine and pas it to AVSpeech. But it´s only *speekone that get passed to the *combine string. So I only ge the first string spoken. Am I declaring the string wrong somehow or do I need to change my method?
- (IBAction)UIButtonPlayPressed:(UIButton *)sender{
NSString *speekone = _activity.activityName;
NSString *speektwo = _activity.activityDescription;
NSString *combined = [NSString stringWithFormat:speekone, speektwo];
if (speechPaused == NO) {
//Title for button [self.UIButtonPlay setTitle:#"Pause" forState:UIControlStateNormal];
[self.synthesizer continueSpeaking];
speechPaused = YES;
NSLog(#"playing");
} else {
//Titleforbutton [self.UIButtonPlay setTitle:#"Play" forState:UIControlStateNormal];
speechPaused = NO;
[self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
NSLog(#"paused");
}
if (self.synthesizer.speaking == NO) {
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:combined];
[self.synthesizer speakUtterance:utterance];
The first argument to your stringWithFormat is not a format specifier.
This line:
NSString *combined = [NSString stringWithFormat:speekone, speektwo];
should become:
NSString *combined = [NSString stringWithFormat:#"%# %#", speekone, speektwo];

An Objective-C message was sent to a deallocated 'UITransitionView' object (zombie) at address: 0x1f120730

I'm facing this issue seldon on this line in the image.
So please help me out. I've spend too much to find out. But i'm not able to find out it.
This is the actual error [UITransitionView isDescendantOfView:]: message sent to deallocated
This is the method in which im doing..
-(void)loadImageAtURLing:(UITapGestureRecognizer*)recogniser
{
[MBProgressHUD hideAllHUDsForView:appDelegate.window animated:YES];
int imgTag = [recogniser.view tag];
NSString *str = [arrDropPinId objectAtIndex:imgTag];
urlString = [arrLargePhoto objectAtIndex:imgTag];
NSLog(#"Larget Image %#",arrLargePhoto);
self.pdvc = [[PhotoDetailVC alloc] init];
self.pdvc.isFromTVView = NO;
if([urlString rangeOfString:#".mp4"].location == NSNotFound)
{
NSString *strImage = [arrLargePhoto objectAtIndex:imgTag];
NSRange lastValue = [strImage rangeOfString:#"300" options:NSBackwardsSearch];
if(lastValue.location != NSNotFound) {
strImage = [strImage stringByReplacingCharactersInRange:lastValue
withString: #"800"];
}
self.pdvc.isVideo = NO;
self.pdvc.imgURL = [strImage copy];
self.pdvc.pinIdStr = [str copy];
self.pdvc.imageURLArr = [arrLargePhoto mutableCopy];
self.pdvc.btnIndex = imgTag;
}
else
{
self.pdvc.isVideo = YES;
self.pdvc.imgURL = [urlString copy];
self.pdvc.pinIdStr = [str copy];
self.pdvc.btnIndex = imgTag;
NSLog(#"DropPIn ARR %#",arrCheckPinId);
}
[self presentViewController:self.pdvc animated:NO completion:nil];
}
Finally i got the solution. Because this is happening UITransitionView and UITransitionWrapperView not setting the frame. So i've done this
self.tabBarController.view.superview.frame = self.view.bounds;

Saving NSAttributedString with NSTextAttachment into file. How to?

I have a NSTextView, which may contains rich text or rich text with image as NSTextAttachment. There is how I adds attachment:
NSImage *image = [NSImage imageNamed:#"image"];
NSTextAttachmentCell *attachmentCell =[[NSTextAttachmentCell alloc] initImageCell:image];
NSTextAttachment *attachment =[[NSTextAttachment alloc] init];
[attachment setAttachmentCell: attachmentCell ];
NSAttributedString *attributedString =[NSAttributedString attributedStringWithAttachment: attachment];
[[aTextView textStorage] beginEditing];
if ([aTextView shouldChangeTextInRange:NSMakeRange([aTextView selectedRange].location, 0) replacementString:#""]) {
[[aTextView textStorage] insertAttributedString:attributedString atIndex:[aTextView selectedRange].location];
[aTextView didChangeText];
}
[[aTextView textStorage] endEditing];
My -fileWrapperOfType:error: method:
- (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName error:(NSError *__autoreleasing *)outError
{
NSRange documentRange = NSMakeRange(0, [[[WindowController aTextView] textStorage] length]);
NSTextStorage *text = [[WindowController aTextView] textStorage];
NSFileWrapper *resultWrapper = nil;
if ([typeName compare:#"public.rtf"] == NSOrderedSame) {
resultWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:[text RTFFromRange:documentRange documentAttributes:[NSDictionary dictionaryWithObjectsAndKeys:NSRTFTextDocumentType, NSDocumentTypeDocumentAttribute, nil]]];
}
else if ([typeName compare:#"com.apple.rtfd"] == NSOrderedSame) {
resultWrapper = [text RTFDFileWrapperFromRange:documentRange documentAttributes:[NSDictionary dictionaryWithObjectsAndKeys:NSRTFDTextDocumentType, NSDocumentTypeDocumentAttribute, nil]];
}
return resultWrapper;
}
But when I'm saving RTFD, all attachments looses. Please, help. What I'm missing?
I have found an acceptable solution, it's described here: Cocoa: custom attachment in a text view

if loop that running asynchoursly

Hello everyone, I want to create if loop that running asynchoursly with this code:
NSString *care = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://localhost/untitled.txt"]];
if (care == #"ONRED") { //untitled.txt = ONRED
[red_on setHidden:NO];
[red_off setHidden:YES];
}
How I can run the if statement like a loop?
If I get you right, you can put those statements in a method and call performSelectorInBackground:
(void)asyncMethod {
NSString *care = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://localhost/untitled.txt"]];
if (care == #"ONRED") { //untitled.txt = ONRED
[red_on setHidden:NO];
[red_off setHidden:YES];
}
}
// in some other method
[self performSelectorInBackground:#selector(asyncMethod) withObject:nil];
Another option is to use the grand central dispatch (as described in this answer):
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
(unsigned long)NULL), ^(void) {
NSString *care = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://localhost/untitled.txt"]];
if (care == #"ONRED") { //untitled.txt = ONRED
[red_on setHidden:NO];
[red_off setHidden:YES];
}
});

how to make one string from several? Xcode

I make some method, that takes digits and operands from stack and display it in a more user friendly style. The problem is with descriptionString variable, it return null in part when topOfStack is "+". I show a log below.
+(NSString *)descriptionOfTopOfStack:(NSMutableArray *)stack
{
NSString *descriptionString;
id topOfStack = [stack lastObject];
NSString *secondInStack;
NSString *thirdInStack;
if (topOfStack)
[stack removeLastObject];
if ([topOfStack isKindOfClass:[NSNumber class]]) {
descriptionString = [topOfStack stringValue];
}
else if([topOfStack isKindOfClass:[NSString class]]){
if(([topOfStack isEqualToString:#"+"]) || ([topOfStack isEqualToString:#"—"])){
secondInStack = [self descriptionOfTopOfStack:stack];
thirdInStack = [self descriptionOfTopOfStack:stack];
descriptionString = [descriptionString stringByAppendingFormat:#"%# %# %#",thirdInStack,topOfStack,secondInStack];
NSLog(#"description is %#",descriptionString);
}
}
return descriptionString;
}
i made example with 2 + 6, this is log:
2012-02-21 22:09:39.983 Calculator[12536:f803] stack = (
2,
6,
"+"
)
2012-02-21 22:09:39.983 Calculator[12536:f803] description is (null)
Why descriptionString is null? Where i made a mistake? Thanks
In the line:
descriptionString = [descriptionString stringByAppendingFormat:#"%# %# %#",thirdInStack,topOfStack,secondInStack];
The variable descriptionString is nil. Replace that line with the following.
descriptionString = [NSString stringWithFormat:#"%# %# %#",thirdInStack,topOfStack,secondInStack];
I suspect you are not setting a value to the descriptionString variable in the other branch of the if, so you are appending string to null. You could set the variable, or you could use [NSString stringWithFormat:format]:
if ([topOfStack isKindOfClass:[NSNumber class]]) {
descriptionString = [topOfStack stringValue];
}
else if([topOfStack isKindOfClass:[NSString class]]){
if(([topOfStack isEqualToString:#"+"]) || ([topOfStack isEqualToString:#"—"])){
secondInStack = [self descriptionOfTopOfStack:stack];
thirdInStack = [self descriptionOfTopOfStack:stack];
descriptionString = [NSString stringWithFormat:#"%# %# %#",thirdInStack,topOfStack,secondInStack];
NSLog(#"description is %#",descriptionString);
}
}