UIRefreshControl weirdness - ios

Just started to use "UIRefreshControl" today, and noticed some weird things.
If I created the UIRefreshControl in InterfaceBuilder, and wire it all up, my selector never gets called. IB says that the control should fire on the "value changed" message, and should call my code, but never does.
So I abandoned doing this in Interface Builder, and just decided to do this in the code. Not too many lines. So I put this code inside of my "viewDidLoad" routine:
self.refreshControl = [UIRefreshControl new];
[self.refreshControl addTarget:self action:#selector(doStuff:) forControlEvents:UIControlEventValueChanged];
NSMutableParagraphStyle* paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.alignment = NSTextAlignmentCenter;
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:#"Pull to refresh" attributes:#{NSParagraphStyleAttributeName : paragraphStyle}];
It draws fine, but the refresh-control is visible when the view first appears. I'd prefer that it not be visible until the user actually pulls down on the table. I'm making a call to a web-service, and wait for the results to come back, then I populate the table. So, you only see the control for a second or two. But it just looks weird.
So, the questions are:
1. Is this a known bug that you can't wire up the control solely from Interface Builder?
2. Is there a way to add this control to the table-view controller, without it appearing until the user actually wants to see it?

You don't need to add a refresh control, or declare one in your viewController. Adding pull-to-refresh is a two-step process.
Step 1: In your storyboard, go to your tableViewController and, where it says "Refreshing", select "Enabled".
Step 2: Add the following code to your tableViewController.m file, in viewDidLoad:
[self.refreshControl addTarget:self
action:#selector(refresh)
forControlEvents:UIControlEventValueChanged];
That's the entire process, other than doing stuff in your -refresh method. There's no more to it.

Yes, there is a bug with the UIRefreshControl that the action is never called. Keith Harrison talks about this in his blog entry UIRefreshControl Fun and Games:
Investigating with the debugger it seems that the refresh method is
never called. I can only assume that for some reason the storyboard is
not properly connecting the UIRefreshControl to our action or is
having some other strange interaction with the
UIControlEventValueChanged event. I have filed a bug report with Apple
(rdar://14178445) but there is also a simple workaround you can use.
The workaround is as described by #AMayes.

Related

Can I connect xib object to any viewController?

I know it is possible to connect an object in a XiB file (i.e button) and connect it to any viewController. I am also able to go to that viewController and programmatically set properties to that object(everything autocompletes fine, it recognizes the object properties) However, when I run the app, the button is unchanged, what gives?
Is there something I'm missing? Is there an additional step that I need to do when using a ViewController that is not the .m file related to the XIB?
Here's part of the code... I don't get any errors!
user.default_sales_rep_id = 2;
if (user.default_sales_rep_id > 0) {
buttonMask.backgroundColor = [UIColor blackColor];
}
You are most likely setting the properties on the button too early. Since you don't specify in your question where this code is located, it's hard to say but I'd guess if you put your code in awakeFromNib it would work.
- (void)awakeFromNib {
[super awakeFromNib];
//code here
}
Any changes to your view that differ from your XIB should be done in this method as it is called after the view is set up from the XIB.
Are you certain you are calling [[UIButton alloc] init] before you attempt manipulating it? I assume you have the button already as an IBOutlet, but if I recall, if you wish to make custom changes to the button, you must still do this.

New view from UIBarButtonItem?

So I've seen previous questions similar to this but they were of no help. I've read Apple's documentation too but I could not understand where I've gone wrong. AFAIK I did everything logically, but when I click on my done button on an UItoolbar overlay, the button can be pushed but it does not do anything. This obviously means it fails to acknowledge the written code. But how?
I want to bring up the .nib of "TableViewController" when a done button is clicked on my UIToolBar. But the below isn't allowing the click to bring up a new view. How do I rectify this? Please show me where I went wrong and what should be replaced and why.
-(void)doneButtonPressed {
TableViewController *UIView = [[TableViewController alloc]
initWithNibName:#"TableViewController" bundle:nil];
UIView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:UIView animated:YES];
[UIView release];
}
Whoa, you've got some bizarre stuff going on here. In your first line, you're allocating and initiating the TableViewController instance correctly, but you're not giving that instance a unique name. You're naming it with another class's name, which is bound to stir up problems. In fact, I'm surprised it didn't through an error.
Try the following instead:
TableViewController *tableView = [[TableViewController alloc]
initWithNibName:#"TableViewController" bundle:nil];
tableView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:tableView animated:YES];
Now, your TableViewController instance has a unique name that is referenced throughout the rest of the method. Just to be clear--UIView is another class name, and therefore cannot be used as the name of an instance of an object.
EDIT: Additionally, be sure to add your button's selector doneButtonPressed: to your .h file of its view controller. Also, if you like you can toss an NSLog() call in the beginning of the function just to be sure it isn't (or perhaps is) being called.
Something to check when button actions aren't firing is that you've got the appropriate selector. If you've followed the selector correctly. Make sure you aren't using a selector of
#selector(doneButtonPressed:)
which would look for a function like:-
- (void) doneButtonPressed:(id) sender
For your member function, you need
#selector (doneButtonPressed)
The debugger is your friend here. Start with a breakpoint to make sure your function is being called.
If you're getting into the function, then The Kraken's answer is the next thing to check.
There is no restriction on using a class name as a variable name whatsoever. Although you should change it because its confusing and doesnt follow iOS coding conventions.
"Button can be pushed but doesnt do anything", is the selector even being called?
-(void)doneButtonPressed
Show how you created the UIBarButtonItem to verify that you provided the right selector in the init method or that you connected the button directly in interface builder (which it doesnt look like since you didnt use the (IBAction) return signature.

iOS automation : how to tap button that has no id

I have the following items in logElementTree output:
UIAButton: rect:{{20, 427}, {41, 41}}
UIAButton: rect:{{140, 427}, {41, 41}}
These buttons have no identifier, no name, and are not drawn in XIB. On my automation test script I only use index (something like target.frontMostApp().mainWindow().buttons()[7].tap())
But then, this line will not always work because index is changing. I just want to ask, if there's a way to tap this button other than using index? Please note that the button has no name, so I cannot use buttons()["name'"].tap()
Technically this would be the best way to go about doing what you would like, so I'll leave it here for other developers who see this question. In your case since you have limited technical experience, I would recommend asking your developer to assign ids or names to buttons. Providing good names for buttons and other UI elements means that your app is also accessible for those users with impaired eyesight, since voiceover will read them the names you've given to your buttons. The following block of code will programmatically assign a label and a accessibility hint to a dynamically created object that conforms to UIAccessibility.
From Apple's documentation (in this case they're doing it on a view, but you could do it on any object like a button):
- (id)init
{
_view = [[[MyCustomView alloc] initWithFrame:CGRectZero] autorelease];
[_view setIsAccessibilityElement:YES];
[_view setAccessibilityTraits:UIAccessibilityTraitButton];
[_view setAccessibilityLabel:NSLocalizedString(#"view.label", nil)];
[_view setAccessibilityHint:NSLocalizedString(#"view.hint", nil)];
}
http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/iPhoneAccessibility/Making_Application_Accessible/Making_Application_Accessible.html#//apple_ref/doc/uid/TP40008785-CH102-SW5
Javascript "hack" (not very clean, but it works...):
var window = UIATarget.localTarget();
window.tap({x:yourXCoordinateHere , y:yourYCoordinateHere});

"self.delegate = self" not working on iOS using ARC

I am working on an iOS SDK 4 project with ARC enabled.
My class MyTextView (derived from UITextView with UITextViewDelegate protocol) implements the following static method:
+ (void)showInViewController:(UIViewController*)viewController
{
MyTextView *textEdit = [[MyTextView alloc] init];
textEdit.delegate = textEdit;
[viewController.view addSubview:textEdit];
// Show the keyboard
[textEdit becomeFirstResponder];
}
In one of my view controllers I call the following:
[MyTextView showInViewController:self]
This crashes with warning: Unable to restore previously selected frame. on becomeFirstResponder. Looks like some stack related crash because of some cycle. I am fairly new to ARC. The delegate property of UITextView is defined as assign (shouldn't ARC interpret that as weak?). I know this approach is rather strange memory-wise. However, I wanted to know if ARC can handle things like that. Obviously it can't. Any idea what might be the problem and how to solve it?
I don't think it has anything to do with the ARC and memory management, but just a more fundamental problem that a UITextView cannot be a delegate of itself. It gets locked in a loop. Put a logging message in textViewDidChangeSelection and you'll see it gets repeatedly invoked. Not a memory issue, methinks, but rather just a logic issue with UITextView delegates. Even if you don't do your problematic showInViewController but just create a standard UITextView subclass and try to set its delegate to itself, you'll see the same curious behavior.
old post, but here is the answer:
http://www.cocoabuilder.com/archive/cocoa/282093-uitextview-as-its-own-delegate-infinite-loop-on-keyboard-select.html
or here aswell
self.delegate = self; what's wrong in doing that?

UILabel text not being updated

I am unable to change the UILabel text. The code for the the UILabel inside viewDidLoad is :
startLabel=[[UILabel alloc] initWithFrame:CGRectMake(75, 395, 200, 30)];
startLabel.text=#"Recording Sound ...";
startLabel.backgroundColor=[UIColor clearColor];
startLabel.textColor=[UIColor whiteColor];
startLabel.font=[UIFont boldSystemFontOfSize:17];
[self.view addSubview:startLabel];
Later, if I want to change the label of the text with the following code, its not changing on the app :
startLabel.text=#"Searching Database ...";
or
[startLabel setText:#"Searching Database ..."];
The UILabel is not empty, I printed it out during debugging and it shows :
(gdb) po startLabel
<UILabel: 0x2c1a30; frame = (75 395; 200 30); text = 'Searching Database ...';
clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x2ae8f0>>
So, the text of the label changes inside the UILabel, but its not updated on the screen.
Can anyone kindly let me know what
I am missing here ? Thanks.
Edit 1: I tried performSelectorOnMainThread: - didnt work for me.
Edit 2: I am using AVFoundation and ASIHTTP classes to record sound and upload the recorded file here. Nothing else. Didnt use any thread.
You may be facing an issue with threading as mentioned in the comments above. If you have a method that runs on the main thread and does some activity (such as search a database), updates that you make to the UI will not be committed until the run loop gets control. So, if you have a long, time consuming task going on on the main thread, run this code after setting the text of the label:
- (void)doSomethingTimeConsuming
... consume some time ...
... set text of label ...
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
... continue long operation ...
}
This should flush out any UI changes that you have made. Although this may be a sensible and functional hack, it doesn't beat the alternative. I highly suggest that you perform your app's time consuming tasks on a background thread, and update the UI through the main thread using performSelectorOnMainThread:withObject:waitUntilDone:. See Apple's page on iOS Thread Management.
In my case the function that was updating was called from a touch recognizer on a thread, but the place in the function where I'm changing the value of the label's text property I put it back on the main thread:
dispatch_async(dispatch_get_main_queue(), ^{
[self.someLabel setText:someString];
});
I had a UILabel showing a level number that would not update to the new level number on a UIViewController. The only viable solution I could find that worked was to call setNeedsDisplay on the main thread of the view controller that owned the UILabel
-(void)changeLevelLabel:(int)theLevel {
dispatch_async(dispatch_get_main_queue(), ^{
self.levelLabel.text = [NSString stringWithFormat:#"%d",theLevel];
[self.levelLabel setNeedsDisplay];
});
}
See http://iosdevelopmentjournal.com/blog/2013/01/16/forcing-things-to-run-on-the-main-thread/ for a more detailed explanation
Mine's a bit more unexpected, though reasonable--just not something anyone thinks too hard about.
I'm supposed to update the UILabel when I receive an NSNotification that I fired somewhere else. However, the notification was fired from a background thread so even the listener methods that update the label's text are fired in the background.
If you're relying on an NSNotification to update the label, or any of your views, do fire the NSNotification from the main UI thread.
In case you are using localize-Swift cocoapod, If you set a localized key to the UILabel, The value will be set after you set any text to the label.

Resources