I am new to iphone programming and I have created a special scenario. I am pretty sure that it will answer most of my questions for creating my app. Here it is. (i HAVE Xcode 4.2)
I have created a universal application with storyboard and single view.
I have appDelegate, storyboard files and a class file ViewController.h/m for my initial ViewController.
Suppose I have added a progress View (Graphically) on the view Controller. Also I have added another viewController on storyboard and made its background black.
When i run the app my first viewController with progress view shows up
Now my questions are
1- How can I link my progress view in the class file and how can i set it progress for 5 seconds.
2- After showing progress within 5 seconds it should switch to other view Controller with black background.
3- I have created a class file "MySecondController" How can i link this class to my black screen viewController.
These are easy questions. I hope I get answer to these. If anyone have tutorials linking to these questions do post. I have tried and haven't found useful.
Best Regards
1a) On the upper right of the xcode window, there's a tuxedo icon. This is the assistant editor. Select the storyboard, select the ViewController, then select the assistant editor. A second editor will appear. Make sure it's editing ViewController.h. Press control and drag from the progress view in the storyboard to a point inside the ViewController interface definition. (right after the line that looks like this: #interface ViewController : UIViewController) You'll be prompted to type an outlet name. Type something like "progressView" (no quotes).
1b) See below to set the progress view to a value.
2) See below to present the second view controller after a delay.
3) In the storyboard, drag a new viewcontroller onto the canvas. On the identity inspector (try the icons under the tuxedo icon, the third from the left is identity), set it's class to MySecondController. Select the original ViewController, press control and drag from it's status bar (the top of it) to the viewcontroller you just added. This will create a segue. A menu will appear asking what kind of segue you want. Make the segue modal for now. Click on the new segue to select it, then click on the attributes inspector (right next to identity inspector) give the segue an identifier, call it "MySecondControllerSegue".
Back to questions 1b and 2) Go to ViewController.m and add this code...
#synthesize progressView;
// this creates methods on self to access the progress view
// this is called after the view appears
- (void)viewDidAppear:(BOOL)animated {
// do inherited behavior
[super viewDidAppear:animated];
// set the progress view value to a number between 0.0-1.0, representing percentage
// notice how we used the synthesized getter: self.progressView
self.progressView.progress = 0.5;
// call another method with no parameters after five seconds
[self performSelector:#selector(doSegue) withObject:nil afterDelay:5.0];
}
- (void)doSegue {
// run the segue that you created in IB
[self performSegueWithIdentifier:#"MySecondControllerSegue" sender:self];
}
Build and run.
After this is working, you'll probably want to show that progress view advancing from the 0.0 position to 1.0 over the course of five seconds. For that, you'll need to read about NSTimer. You can set one up to send you a message periodically, and you can adjust the progressView each time.
Related
I'm a beginner to iOS development, so forgive me if this is really basic. It's probably answered somewhere, but I've looked for a long time, and I'm struggling.
I have a second View on my story board that I've successfully linked to the first view using a Navigation controller and stuff, and I'm able to navigate to it. I can also add actions/outlets from elements on my first view by Control-dragging to the .h file.
I have a label on my second view, and I want to be able to do the same: add actions and outlets from elements. But when I try Control-dragging, nothing happens. What am I doing wrong, and how do I fix it?
Hmm, well first of all welcome to Stack Overflow! And thanks for asking the question.
Let me know if I have this right - you have two view controllers to the right (linked with segues) of a navigation controller and currently you can navigate to the second from the first using a button at the top right? Then when on the second view controller there's a nav button at the top left with a little arrow by it? And this should take you back to the first. Is that right?
Now on the second view controller you want to create a button that performs an action, but when you right-click-drag (ctrl-drag) onto a .h or .m file nothing happens?
If that's the case I've seen a few reasons for that. You might try:
You need to make a button, a label can only recieve actions, not create them. Read this article on IBOutlet vs IBAction
Restart Xcode (I know, it's lame, but humor me)
Make sure you're dragging (if on the .h file) between "#interface" and "#end"
Make sure you're dragging (if on the .m file) between "#implementation" and "#end"
Do you have a custom view controller class for your second view? If so, select the second view controller in your storyboard and go to the identity inspector. Set the custom class to your custom view controller's class name. Now you will be able to control-drag IBOutlets and IBActions.
I'm trying to execute code when a button is pressed for an application but I can't find how to change the views after the code is executed. Is there a way to switch views how I want to or is there another way? Thank you in advanced, I'm very new to xcode.
edit: I'm trying to go from one view to another, not the view controller and yes I have one storyboard that I planned on using for the whole project if possible.
To execute code when a button is pressed, you have to set up a method that the button is hooked into. Because you said you're using storyboards, I'll assume your button is on the storyboard. Go to the assistant editor, hold ctrl, and click-and-drag from the button to the view controller's .m file (#implementation section). This will create an IBAction method, and any code in this method will execute whenever you press the button.
The method will look like this:
- (IBAction)aButtonPress:(id)sender {
}
According to your comments, you say you only want to change the on-screen view, and not transition from one view to the next.
Views are added and removed with the following methods:
[aSuperview addSubview:aSubview];
[aSubview removeFromSuperview];
I can't really tell you much else with a lot more detail from you... and even though I asked twice in the comments and you said you only want to change the view, not the view controller... I think you probably need to transition to a new view controller. Impossible to know for sure as you've given almost no detail...
But if you want to transition between view controllers (which also transitions views), then ...
Create two view controllers on the storyboard. Hook them together with a segue, and give that segue a name. Now, to perform that segue in code:
[self performSegueWithIdentifier:#"YOUR_SEGUE_NAME" sender:self];
I'm trying to create a second view controller and link it to a new set of .m & .h files then alter the text of a label on that view controller under the viewDidLoad but am having some issues.
Here's what I am doing so far.
I'm starting with a blank new single page, from the MainStoryboard, I add a new VC and add buttons so I can navigate between the pages. This works fine.
Next I create a new Objective-C Class, "SecondViewController" and choose subclass UIViewController.
Now back on the Storyboard I select the other ViewController and try to pick the new SecondViewController as a Custom Class but it doesn't show up in the list of available classes.
Now, if in step 2 I choose the UIView subclass I can assign it to the ViewController in step 3 but the viewDidLoad options are not available so I am not sure how to program changes to the VC.
What am I missing here?
I am able to use the UIView and use the following to make the change I am asking about. I am not sure if this is the correct way to do this.
- (void)drawRect:(CGRect)rect
{
myLabel.text = #"change2";
}
EDIT ----
Okay, I've figured it out. I was clicking on the VC's View (background) in the Storyboard and not the actual ViewController! Now it works the way I thought it should. I suppose if I had looked at the View Controller Scene I would have noticed what I was doing incorrectly.
Now, if in step 2 I choose the UIView subclass I can assign it to the
ViewController in step 3 but the viewDidLoad options are not available
so I am not sure how to program changes to the VC.
The class of your ViewController (or similar one) from interface builder and another class in your code must extend the same class
I am creating a project with Xcode 4.2 and using it's storyboard. In one of the views I have a button that a user will tap and it will perform some calculations. If the calculations are correct I need to display the view that they just came from. I am currently using a Navigation Controller. When the app starts it loads View 1. When I choose an option it loads View 2. If I click the 'back' button in the toolbar it loads View 1 with left animation.
In my IBAction method for the View 2 calculation I have the following snippet.
[self performSegueWithIdentifier:#"BackToView1" sender:sender];
But the problem is it loads View 1 using the left animation when I need it to load the right.
Would that be a custom segue? Am I missing something?
[Edit]
I just noticed something as well. When View 1 loads from my IBAction it appears it is initializing the AppDelegate again, whereas the 'back' button does not. I need to not initialize the AppDelegate since I am loading a data object at the start of the app. Calling init again kills my object.
You could try using the navigation controller to pop back. This will probably produce the effect you are after.
[self.navigationController popViewControllerAnimated:YES];
I asked a question on here last week and I have tried to follow examples but not having much joy,
So I am trying to create a app that will have a main window. In this window I want to be able to display 3 or 4 different views (not at the same time) which the user can select the view via a button press. I did not want to use a navigation bar if possible.
am I right in thinking that I will need
View1 .h &.m
View1controller .h &.m
and the same for the second etc
Now if the button to select view 2 is on view 1,
how do I change the view and where do I put this code?
Do I need a root-controller or am i getting confused with other ways of doing the same thing.
Thank you in advance for your help.
If you want to change between the views like in the iPod application, you can accomplish this with a UITabBarController. You would create all the view controllers, and add then to a root tabbarcontroller. The tabbarcontroller then serves to automatically switch between views for you when the button is selected.
If you have a view with a button on, and want to change the active view u can use this method from UIViewController of the active view:
- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated;