I am using storyboard and kal calendar controller, I want to customize the event of choosing one day on the calendar. By default when you choose one day events on that day appears in the table view under the month calendar. What i am trying to do is when choosing one day another view controller is showing up and it is filtered on the day chosen.
Until now I founded where I should edit. But I can't call the other view to show up !!
I tried this but it dose not work !!
tableViewController *tbl = [[tableViewController alloc] initWithNibName:#"menuView" bundle:nil];
[self dismissViewControllerAnimated:YES completion:nil];
I found this but I do not know how to use it and if it is helpful for me or not .. any help ?
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"menuView" bundle:nil];
tableViewController *vc = [sb instantiateViewControllerWithIdentifier:#"menuView"];
The second batch of code in your question is headed in the right direction, but not exactly right.
First, you want to get an instance of your project's storyboard. That's what you're attempting to do in the first line of code. However, I believe you're referring to your storyboard by the wrong name. Usually Xcode defaults the name of storyboards to 'MainStoryboard.storyboard', while you're trying to refer to it as 'menuView'. So you'll want to change that first line of code to this:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
Next, you need a way to refer to the ViewController you want in the Storyboard. That's what you're attempting in the second line of code. You need to make sure you're getting a proper reference to it:
In your storyboard, select the ViewController you're wanting to show up
In the Identity inspector, enter "tableViewController" in the Class field
Enter "menuView" in the Storyboard ID field
Finally, you'll need to add one line of code to actually present the ViewController.
So all in all your code should look something like this:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
tableViewController *vc = [sb instantiateViewControllerWithIdentifier:#"menuView"];
[self presentViewController:vc animated:YES completion:nil];
I am using UIActivityIndicatorView for the first time in an iPhone app, in order to let the user know that some process is on the way.
To do that I use the detachNewThreadSelector method (also for the first time).
I end up with code like this:
[myActivityIndicator startAnimating];
[NSThread detachNewThreadSelector:#selector(theWorkToBeDone:) toTarget:self withObject:myObject];
The problem is that when I time the application. It takes about 5 times longer to perform the task when I use the code above than when I do not use UIActivityIndicatorView and detachNewThreadSelector. (In which case the user still waits, but a shorter time).
Is this difference in timing what I should expect when using UIActivityIndicatorView ?
Or am I making some beginner mistake due to my inexperience with both UIActivityIndicatorView and detachNewThreadSelector?
Thanks for any bit of information.
I just found out that adding this line a the beginning of the theWorkToBeDone method:
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
and this one at the end:
[pool release];
solves my problem.
I find it very tedious having to create connections and other repeatable stuff in XCode 4. This just does not seem RAD to me. Let me explain
I have a View based iPad project with a single view.
On the view are about 50 buttons and 50 UIImage objects - total 100 objects. All the UIImage objects have the same image. All the buttons have the same Action. The tag of each button determines the code to run in the Action.
What I find tedious is:
I have to type in a "tag" for each of the objects - 100 tags
I have to drag each object to make an outlet which produces code like this in the .h file:
#import <UIKit/UIKit.h>
#interface bbTestViewController : UIViewController {
UIImageView *pop1;
UIImageView *pop2;
UIImageView *pop3;
UIImageView *pop4;
UIImageView *pop5;
etc....
for the 50 controls and also this code below:
#property (nonatomic, retain) IBOutlet UIImageView *pop1;
#property (nonatomic, retain) IBOutlet UIImageView *pop2;
#property (nonatomic, retain) IBOutlet UIImageView *pop3;
#property (nonatomic, retain) IBOutlet UIImageView *pop4;
#property (nonatomic, retain) IBOutlet UIImageView *pop5;
etc....
for the 50 controls.
and in the .m file:
#synthesize pop1;
#synthesize pop2;
#synthesize pop3;
#synthesize pop4;
#synthesize pop5;
etc.....
This also adds lines like this:
- (void)viewDidUnload
{
[self setPop1:nil];
[self setPop2:nil];
[self setPop3:nil];
[self setPop4:nil];
[self setPop5:nil];
etc.....
and
- (void)dealloc {
[pop1 release];
[pop2 release];
[pop3 release];
[pop4 release];
[pop5 release];
etc.....
3.Then for each of the 50 buttons I have to drag them to the the same Action to make the connection, select the touch action, type a name and then click OK.
This I find tedious and counter productive. Not RAD at all. More SAD. So much of repeating the same actions, it is painful.
So the Question is, is there an easier way. I do not believe in Copy/Paste so that solution is a no-no.
In other languages I would use a For...Each to iterate thru every object and do the needful.
e.g. for the tag:
I would have code to set the tags like this:
Integer n = 1;
For Each oObject in myView
oObject.tag = n
n = n+1
end for
So can I use code to get around these tedious processes of
tag
Declaring each object and adding the #property... code
adding the #synthesize... code
setting each object to nil
releasing each object
Somehow automate the connection from all the buttons to the Action.
I want to be able to do all this in code and without all that copy/paste stuff or those repeatable actions in IB for XCode 4 by dragging to make a connection.
Is this at all possible in this MVC system that XCode 4 uses? If so, how?
Update
After some research I found at least one answer to my Q - with regard to Connections to Actions.
It appears that if you add an object - say a button and make a connection to an action in IB, then if you copy and paste that button (or any such object), IB RETAINS the connections made so in my case above, I create the button and make the connection to the Action and if I copy and paste it, IB will make the connections to the same Action for every pasted button. At least that will save some time. HTH others.
You are right about one thing IB sucks royally. I had high hopes that Apple would have done more to fix it in XCode 4.x.
However I do wonder about your interface. Having 50 buttons on one view seems to be a little much. So you might want to refactor that aspect of your project.
As to your current issue, do realize that you can build an app without IB. You do need to learn how to programiclly build the controls and do the layout. I'd suggest looking at sample code from Apple first.
Still I'd suggest thinking long and hard about those 50 buttons.
In the view controller do:
for (int i = 0; i < 50; i++) {
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
xpos = ...; // appropriate coordinate go here
ypos = ...; // appropriate coordinate go here
[button setFrame: CGRectMake(xpos, xpos, buttonWidth, buttonHeight)];
[button addTarget: self action: #selector(myCoolAction:) forControlEvents: UIControlEventTouchUpInside];
[button setTag: i];
[[self view] addSubview: button];
}
and you should be fine. Same for the images...
there is a sort of halfway house approach that i have used in one of my apps, where i wanted to be able to address buttons in a grid using an array in code, and like you, i wanted to design the look and feel in IB.
i did what you did in the sense that i put all the buttons on the view, and went as far as declaring an ivar, #property and #synhesize declaration for each one. since they were in a grid like you have described, what i did was name each one along the lines of button_1x1 button_1x2 etc (ie button_rxc as row x column if you think of a spreadsheet. the names are not important as you are only ever going to refer to them once in a method you call from viewDidLoad.
in that method, i created a bunch of NSArrays that held each button. for example:
landscapeHidden = [[NSArray arrayWithObjects:
button_3x1, button_3x2,
button_4x1, button_4x2,
nil] retain];
portraitHidden = [[NSArray arrayWithObjects:
button_1x3, button_1x4,
button_2x3, button_2x4,
nil] retain];
landscapeButtons = [[NSArray arrayWithObjects:
button_1x1, button_1x2, button_1x3, button_1x4,
button_2x1, button_2x2, button_2x3, button_2x4,
nil] retain];
portraitButtons = [[NSArray arrayWithObjects:
button_1x1, button_1x2,
button_2x1, button_2x2,
button_3x1, button_3x2,
button_4x1, button_4x2,
nil] retain];
i then used these arrays whenever i wanted to iterate them programatically.
the _hidden arrays are just used to indicate what buttons to hide when the display changes perspective - rather than move them around i figured it was faster to just hide the few that did not fit, and repaint them all.
as for interface builder, try this:
hold down control and click "File's Owner" once, and wait.
you will then get a large panel that you can use to wire connections to the view objects. i prefer using this method regardless of which direction i am connecting things up - ie if you drag from the objects directly the direction you drag in is not always convenient. this way you can easily view all the outlets at once. much easier from my perspective.
having said that it's pretty easy to loop through an array and assign targets or add gesture recognizers/ update titles/ colors/ whatever you like. i suppose you could also store them in a dictionary, (or have an array of dictionaries).
i went the array of having an array of dictionaries and the arrays of buttons you see above. on interface flips, the dictionaries don't change, but the buttons in the arrays get re-tagged, so any methods that have them as a "sender" can immediately get their dictionary data by indexing into the data array with
[myArray objectForIndex: ((UIButton*)sender).tag];
another trick - cmd-b build before you try doing anything in IB. this ensures it picks up things you have just edited, or files you have just added. sometimes i have had to clean/build to force an image to appear in the IB drop-downs. it's usually pretty good, but if you rename an image file, it's usually easier to delete it from the project and drag it in again, build (or clean build) then it should be good to go.
I want to display quite a bit of demographic data for a certain pin when someone touches on it, so providing a pop-up isn't going to cut it. I figured once the pin is touched I will just stick a tableviewController onto the NavigationController and the table view will have access to the object and display the single objects information, with one item per row and 1 section.
Anyway I'm having a hard time figuring out MKMapViewDelegates methods as it appears none of them do what I need and/or allow me to return a tableview or push that view onto the navigation controller.
I played around with:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
But that requires a MKAnnotationView be returned and I really just need this method to work by showing the user a table view of all the data. I was hoping for something simple like a userDidTouchPin method....
Anyone have any ideas how to accomplish what I am trying to do?
If you want to do something when the user selects the pin (and not a button on its callout), then implement the mapView:didSelectAnnotationView: delegate method and present or push your detail view controller there.
The selected annotation object is available as the annotation property of the view parameter:
- (void)mapView:(MKMapView *)mapView
didSelectAnnotationView:(MKAnnotationView *)view
{
YourDetailViewController *dvc = [[YourDetailViewController alloc] init...
dvc.annotation = view.annotation;
//present or push here
[dvc release];
}
You might need to check which type of annotation was selected (eg. if MKUserLocation was selected do nothing, etc) and you might need to cast the view.annotation to your own annotation class to easily access any custom properties you may have.
My dilemma is as follows:
I have a custom subclass of UIImage (I've added some serialization methods i.e. initWithCoder, encodeWithCoder), and I would like to apply my custom subclass as a variable to a UIImageView, or at least a subclass of UIImageView.
As you are aware, UIImageView has a variable called "image", that is of type UIImage. I'd like to override this variable with my subclass.
My goal is to have a UIimageView that will respond to archivedDataWithRootObject without crashing with the encodeWithCoder message is sent to the variable image. If there is a smarter way to get there, I'm open to suggestions.
Thank you!
[edit] I think one possible route is through some type of casting...However:
UIImage *image = [[UIImage alloc] init];
MLImage *mlImage = [[MLImage alloc] init];
mlIamge = (MLImage*)image;
When I mouse-over mlImage after the second line executes, I can see that it is of type MLImage. However, even with the cast, after the third line executes mlImage becomes a UIImage. What do I need to do to change the type of image to MLImage?
[/edit]
Be careful with your terminology. It's not possible to override a property—the UIImageView will still have its own image property, it won't be replaced. You're trying to override the accessors (-image and -setImage:).
And you're right, casting is a better route. But your example has a problem: that cast is illegal. When you do [[UIImage alloc] init], you're creating an object of type UIImage. Nothing is going to change that, including your cast. Casting is a way of telling the compiler (in your case) "I know you think that this UIImage* is, well, a UIImage, but it's really a MLImage." In this case, that's a lie, since it really is a UIImage—you allocated it right there.
What you can do, though, is stick an actual MLImage into the image property of a UIImageView. For example:
MLImage *myImage = [[MLImage alloc] init];
myView.image = myImage;
And then somewhere else:
MLImage *myImage = (MLImage *)(myView.image);
And you'll get back your custom image object. But it has to have been an MLImage in the first place.