问题
Firstly, I am an iPhone programming noob (I know that's not very helpful). But I do speak english :).
I am making an app that is basically a soundboard, that is you press a button and it plays a sound effect.
One thing I wanted to add was a view which came up when a sound was playing, that said something like "A sound is currently being played, please tap anywhere to stop". The way I did this was take a screenshot of the normal screen, put a transparent black square over it in Microsoft Word (I know, great graphics program, right?) and use that. It looks fine.
I was wondering how to make it so that when you press a button to play the sound, not only does it play the sound, but it also switches views to the second view described above. Then, if the person touched the screen in the second view, it would switch back to the first view, and stop playing (I assume the best way to do this would be to have some kind of 'stop playing" method in the "viewDidLoad" section of the first view).
I would very much appreciate it if someone could tell me how to make an action which is called when a button is pushed, that switches views. I don't need any fancy navigation bar staying up or whatever, I just need it to switch views.
Also, my application is a view based application, and I currently have the following files:
Classes: (appname)appdelegate (.h & .m), (appname)viewcontroller (.h & .m), (my main window) playingController (.h & .m), (my second window)
Resources: (appname)viewcontroller.xib (main window IB file) playing.nib (second window IB file) mainwindow.nib (no Idea what this does, don't really care).
I think that these will work for the app, but I'm probably totally wrong. Please tell me if I need additional files, or if I need to rename these files.
Thanks a lot,
Luke
回答1:
Try placing an invisible button over the entire view to get back to the first view
to get to any screen use something like this:
SecondScreen *second = [[SecondScreen alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
Steps: 1 Go to interface builder (double click .xib file) 2 Drag a UIButton on screen 3 Go to code 4 Go to .h file and write
-(IBAction) switchViews:(id)sender;
5 GO to .m file and type in
-(IBAction) switchViews:(id)sender
{
SecondScreen *second = [[SecondScreen alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
6 In interface builder, click the button, Hold CONTROL and Drag all the way (a line shows) to the FileOwner. 7 Choose switchViews method from list
gl
回答2:
You need to create a new UIView
, controlled by a UIViewController
and presented with presentModalViewController
You need a UIButton
, which can be set up either programatically or within IB that will call presentModalViewController
Once your new view is visible, you can set it up however you wish in terms of your 'Stop Playing' action. To remove it, dismissModalViewController
is what you're looking for.
http://developer.apple.com/library/ios/documentation/uikit/reference/UIView_Class/
http://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/
http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/
来源:https://stackoverflow.com/questions/4114999/switch-views-with-button-in-iphone-app