UIActivityIndicatorView not appearing iOS 5 - ios

I am trying to implement a simple spinner while my app seeks data from the Internet. My data grab is working fine, but it sometimes takes a while, so I'd like to use a UIActivityIndicatorView to let the user know that the app hasn't crashed. But it's currently not working. I've gleaned as much as I can from these forums, and others, but for some reason it's not working. Again, I feel like this solution is probably very simple. Here's what I'm doing:
I declare the spinner as a property in the .h file:
#property (strong, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
I have also dragged a spinner object onto the viewcontroller using storyboard, and created a connection to the property using CTRL-drag.
The button that triggers the URL request (and consequent loading & parsing of data) actually triggers a segue, so I figure using the
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
function is as good a place as any to get things started. I don't have a specific function attached to the button apart from the prepareforsegue function. (Maybe this is an issue? does it need to have an IBAction associated?...)
Anyway, that function triggers this:
[self GoAheadAndGetData];
which, in turn, begins like so:
- (void)GoAheadAndGetData {
[NSThread detachNewThreadSelector:#selector(threadStartAnimating:) toTarget:self withObject:nil];
....... etc
So that function looks like this:
- (void) threadStartAnimating:(id)data
{
[activityIndicator startAnimating];
}
And finally, at the end of the GoAheadAndGetData function, I have placed the stop animating call, like so:
[activityIndicator stopAnimating];
Is that clear? Please let me know if anyone needs more info - I have tried to strip it down to basics, so I don't have to post a ton of code here...
Thank you in advance - I'd really appreciate any help I could get. I don't quite know why it's not working at this point.

Related

Creating TableviewController and presenting it

I'm developing IOS messanger app, I have inbox(tableview) in which I have cells(conversations) and when I select a conversation, I would like to present this conversation(tableviewController full of messages), but i dont like how much time it takes to present this controller. So my idea was to create whole controllers(tableviewController full of messages) objects before selecting conversation, and then just push them. First time I select conversation, it is blank, after going back and then selecting it again, it work. Problem is obvious, some variables are initialized in viewDidLoad method. I have tried to move them to init method but then every time conversation was blank.
Do you have any experiences with this? Any hint will be appreciated a LOT.
Thank you!!!
in tableviewController full of messages:
.h file:
#property (nonatomic, assign) BOOL firstAppear;
.m file
self.firstAppear = NO; //in init method
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if (self.firstAppear) {
//add a indicator view here
}
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
if (self.firstAppear) {
//get tableView data here, then [tableView reloadData] to show data
//remove the indicator
self.firstAppear = NO;
}
}
It sounds to me like you are doing premature optimization. Creating and pushing a table view controller should take a small fraction of a second. If it's taking longer, something is wrong. Are you loading the conversation data from a remote server or something?
You might want to use Instruments to figure out what is taking extra time and causing a delay. Then you can focus on the actual cause rather than guessing.

Getting URL of UIWebView

I'm sort of new to Xcode, so forgive me in advance for any obvious wrong things that I might write.
I'm trying to write something a bit simple using the UIWebView. I've already made it load upon the app's loading, but I can't seem to do anything else. What I want to do next is to make a button appear/disappear depending on the current URL. This is what I used to (try to) get the current URL:
NSString *currentURL = viewWeb.request.URL.absoluteString;
(I'm using this code in the ViewController.h file)
When I made the outlet (Ctrl+dragging), I named it viewWeb and I also went and labeled it viewWeb. But it doesn't seem to work and I don't know why.
Also, please don't just give me some code without explaining, because this is a bit frustrating and I want to understand it.
EDIT: Thanks, but I'm not looking for help on the disappearing button just yet (by the way, viewWeb is my UIWebView, not the button). I need help to detect an URL change to make the button disappear. Is there a webViewDidLoad or something similar? viewDidLoad isn't for this.
In the ViewController.h File, you only declare the property, like this:
#property (strong,nonatomic) NSString *currentURL;
Then you can set the value in the ViewController.m file.
maybe in the -viewDidLoad method in the ViewController.m file like this:
_currentURL = viewWeb.request.URL.absoluteString;
You can set the property of hidden or not of the button to show it selectively based on the url.
// URLString contains the URL based on which you would like to show/hide the button
if ([currentURL isEqualToString:URLString]) {
viewWeb.hidden = NO;
}
else {
viewWeb.hidden = YES;
}

I wrote a function in my main view. Now, how do I call it?

I have a function that needs to be called from several different places in my main view. Let's call it updateFunction.
I declare it as such:
- (void)updateFunction {
//updates some variables here
}
This happens immediately after #implementation MainViewController.
Now, I cannot figure out how to call it. [updateFunction]; is wrong, as it updateFunction();.
I know that this is stupid, but it's so basic that I don't think people are really writing about it. Can someone please just tell me how to call the function that I've written?
If you're calling it from a method in MainViewController you'll probably want to call:
[self updateFunction];
If you're calling it from outside MainViewController then in the owning object it would look something like:
MainViewController *mainViewController;
mainViewController = // set it somewhere;
[mainViewController updateFunction];
p.s. - I recommend you start with some Objective-C tutorials. Apple has a bunch on their website if you search for "sample code".
Use this code:
[self updateFunction];

App hangs when property is included in block

I have a UIViewController which displays a table of data that is pulled from an online database. I have a singleton manager to handle the pulling of this data and provide the data the table needs.
This is an example of how the manager works:
#property (nonatomic) NSArray *dataArray;
...
- (void)refreshDataSource
{
[AClass fetchInBackgroundWithCompletionHandler:^(NSArray *objects) {
self.dataArray = [NSArray arrayWithArray:objects];
}
}
...
- (NSArray *)tableViewDataSource
{
return self.dataArray;
}
The view controller requests an update by calling -refreshDataSource in -viewDidLoad but in the meantime provides its UITableView with cache data from the manager by pointing to -tableViewDataSource.
When the view controller presents itself for the first time, everything is fine. The second time I go to present the same view controller, the app hangs. The network request doesn't fire either.
The only fix I've found is moving my -refreshDataSource call to -viewDidAppear: instead. But it itches me why this would be happening and discomforts me that something here must be wrong.
If anyone could provide any help or suggestions that would be great!
Your question hasn't explained everything, but here are a couple of ideas that might help you.
1) viewDidLoad is only called the first time your view loads. If you switch to a different view, then return to your tableView, refreshDataSource will not be called.
2) viewDidLoad might be firing before an array has been allocated and initialised, so it's nil when you're refreshing the data, whereas viewDidAppear might not have the same problem.
I can't give a more concrete answer without more information. Can you explain "the first time, everything is fine. The second time I go..." more clearly? Step-by-step what you do, if possible.

How to show the text of an UITextField in an UILabel?

I am building an interface, where I can add events like in a calendar.
In the AddAEventViewController I have Buttons to set the starttime, duration and recurrence.
Every time you press a button a viewcontroller comes up with a UIDatePicker, where you can set your time. The picked component is than displayed in a UITextField. Now when I press the Done-Button, it dismisses the ModalViewController and I am back to my AddAEventViewController. Next to the Durationbutton e.g. is a UILabel, where I want to show now the just picked and in the textfield shown duration.
How do I get access to the AddEventViewController out of an other ViewController? I tried to alloc and init a new one there, but it didnt work!
- (IBAction)pressedDoneButton:(id)sender {
_mainAddWishViewController.labelDuration.text=textFieldDuration.text;
[self dismissModalViewControllerAnimated:YES];
}
Can someone help me please!
Thank you Jules
There are several ways you can do this, all of them documented here. Reading and understanding them will help you a lot in iOS software development.
There are many ways to achieve this. Here is one that is fairly straightforward.
In the "child" viewController, add a delegate property and set it to the parent view controller.
Then in your Done button handler, do something like:
[self.delegate performSelector:#selector(didComplete) withObject:self]
In the parent view controller, define a method as follows:
- (void) didComplete: (YourSubViewControllerClass *) sender
{
self.labelDuration.text = sender.textFieldDuration.text
}
Basically, this implements an informal protocol whereby the subViewController informs the main view controller that it is finished and input values are available.
Note that if you cancel out of the subViewController, don't send the didComplete message.

Resources