Create Own View Controller I can present modally - objective-c

I want to create an object that I can present modal view, like UIImagePickerController. With UIImagePickerController I create the object, configure it, then present it modally. like:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
//Configure the UIImagePickerController
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: #"public.movie", nil];
imagePicker.videoQuality = UIImagePickerControllerQualityTypeMedium;
imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
//Present the Controller
[self presentModalViewController:imagePicker animated:YES];
I want to do the same thing. I want to have a controller I can instantiate, configure, then present it. I am confused on how to proceed with that though. Do I use a standard UIViewControler (looking into UIImagePickerController header I see apple uses UINavigationController)? How do I connect a view to this controller? Is it a story board or just a xib file. I want to build my controller this way because object can be useful to reuse in other projects so I would love to just be able to send it to the other project with documentation on how to configure it and they can just plug it in and it works (basic object orientation). Are there any basic guides on how to do this?
Thank you for reading and the help.

A good place to start with understanding how to do this would be to see Apple's View Controller Programming Guide for iOS. It gives some good examples and different options on how you could get this accomplished.
Also, have you done/found any simple tutorials that show the process of building a simple application?
Good luck.

Related

Stopping Audio from different class

Note: I have looked at other questions and the answers to them are quite vague or unhelpful
I have this code in View_Controller.h
#property AVAudioPlayer *playerSaxophone;
Then I do this in the same file (in viewDidLoad):
NSURL *backgroundMusicSaxophone = [NSURL fileURLWithPath:
[[NSBundle mainBundle]
pathForResource:#"saxophone" ofType:#"wav"]];
self.playerSaxophone = [[AVAudioPlayer alloc]
initWithContentsOfURL:backgroundMusicSaxophone error:nil];
self.playerSaxophone.numberOfLoops = -1;
[self.playerSaxophone setVolume:0.5];
[self.playerSaxophone play];
In a different view controller I want to be able to stop or start this audio from playing by clicking 2 buttons. Is there any way I can do this?
Edit: I tried this in the "different" view controller .m file
//I do import ViewController.h in this file
- (IBAction)stop:(id)sender {
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[viewController.playerSaxophone stop];
}
But it didn't work.
You need to call:
[playerSaxophone stop];
to make the music stop.
Problem: Another view controller won't have a reference to playerSaxophone.
The fact that you want to add code that references this from another view controller means you'll have to find a way to have the three objects - view controller A, view controller B, and your audio player - communicate.
Here are a few options:
Use the delegation pattern to have one view controller tell the other view controller to stop the player.
The same as #1, but using the notification pattern. (This is ideal if you'd have a 3rd place that controls the player).
You could move playerSaxophone to be a #property on either your App Delegate or a singleton. Then each view controller will be able to find it.
These are just a few solutions. There are others.
Additional reading:
Apple's Coordinating Efforts Between View Controllers
This Stack Overflow answer
How do I set up a simple delegate to communicate between two view controllers?

ios7 uiimagepickercontroller showscameracontrol

I am using UIImagePickerController on iOS7.
I don't want to use basic camera controls, so I set showsCameraControl=NO;
So, I can see not full screen uiimagepickercontroller on my iPhone5s.
The Camera View frame size is that the size before setting showsCameraControl.
And here is my code that define UIImagePickerController in MainViewController
CustomCamera *imagePickerController = [[CustomCamera alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.sourceType = sourceType;
imagePickerController.allowsEditing = NO;
imagePickerController.delegate = self;
imagePickerController.showsCameraControls = NO;
'CustomCamera' is almost same as UIImagePickerController class(has .xib).
How can I solve my problem?
You should read the class reference for UIImagePickerController.
Important: The UIImagePickerController class supports portrait mode
only. This class is intended to be used as-is and does not support
subclassing. The view hierarchy for this class is private and must not
be modified, with one exception. You can assign a custom view to the
cameraOverlayView property and use that view to present additional
information or manage the interactions between the camera interface
and your code.
If you have modifications you wish to make, it must be done solely via cameraOverlayView property. Otherwise, you could use AVFoundation do accomplish this if you want total control over how things look.
How to save photos taken using AVFoundation to Photo Album?

Pushing a new instance of a table view controller object to the top of the stack while using the same nib file

I am learning how to program apps in ios. Anyone out there know of a recent tutorial that shows how to push a new instance of a table view controller object on top of the stack without the need to create and hookup new nib?
I found an old tutorial at iphone SDK Articles dated 3/2009. The website does not exist anymore as its domain expired. But the article, using a pre-packaged 2009 "Navigation-Based Application" in xcode, shows how to drill down a table using the same view controller rather than incorporate a new nib file. In the article, the view controller is identified as a "rootview" controller.
I have managed to modify the code to populate the data from the plist to a table cell using a UIViewController class with UITableViewDelegate and UI TableViewDataSource. This was not difficult to do. However, my problems begin when a cell is activated with the didSelectRowAtIndexPath method. The tutorial's code creates a new instance of the same table view controller class, updates the cell data and pushes it to the top of the stack to effect the "drill down." When I apply the same method to my controller class, xcode throws an error. Admittedly, my ignorance is the likely cause of the problem. That is why I am looking for help.
The tutorial code implementing the didSelectRowAtIndexPath:
//RootViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the dictionary of the selected data source.
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
//Get the children of the present item.
NSArray *Children = [dictionary objectForKey:#"Children"];
if([Children count] == 0) {
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
} else {
//Prepare to tableview.
RootViewController *rvController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:[NSBundle mainBundle]];
//Increment the Current View
rvController.CurrentLevel += 1;
//Set the title;
rvController.CurrentTitle = [dictionary objectForKey:#"Title"];
//Push the new table view on the stack
[self.navigationController pushViewController:rvController animated:YES];
rvController.tableDataSource = Children;
[rvController release];
}
}
It is hard to say for sure since you didn't post the actual error message.
One thing that may be an issue is that you pass Children to your new controller after pushing it to the stack. Therefore, some of the ViewController's view life cycle methods have probably already been called, and if you tried to do something on Children before it was set, that may crash the application.
Change it to :
rvController.tableDataSource = Children;
[self.navigationController pushViewController:rvController animated:YES];
Something else that may help : add an exception breakpoint in Xcode and set it to catch All exceptions on throw (I believe this is the default setting).
Should it crash because of something you wrote, the application will break on the actual line posing problem, which may help you understand the issue.
If this doesn't not help, as said before, be sure to post the actual error message.
I had to instantiate a navigation controller object in the app delegate. Self.navigationController does not work unless there is a navigation controller in play. I have since updated the code to work with storyboards by utilizing the UIStoryboard class.
I feel like I am slowly climbing Mt. Everest with every little step. It's painful for a new programmer (I make my living as an attorney) but somehow I seem to be moving forward.

Can we put navigationcontroller in MasterView in iPad Application?

I have worked mostly on iPhone apps. Now I need to build an iPad app. In that I need to put NavigationController in the MasterView of the Master Detail View of the iPad as we do in the UITableView in the iPhone. I mean when user selects a particular row it should navigate to another tableview with newly filled data. and user can go to the previous by pressing back button. Also On each selection I need to make change of image in the detail view.
I dont have any idea to achieve this.
Please provide any suggestions or any sample code for it.
Thanks in advance.
try this in your appDelegate didFinishLaunchingWithOptions method
MasterViewController *masterViewController = [[[MasterViewController alloc] initWithNibName:#"MasterViewController_iPad" bundle:nil] autorelease];
UINavigationController *masterNavigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
DetailViewController *detailViewController = [[[DetailViewController alloc] initWithNibName:#"DetailViewController_iPad" bundle:nil] autorelease];
UINavigationController *detailNavigationController = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
self.splitViewController = [[[UISplitViewController alloc] init] autorelease];
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers = [NSArray arrayWithObjects:masterNavigationControlle, detailNavigationController, nil];
self.window.rootViewController = self.splitViewController;
[self.window makeKeyAndVisible];
This is certainly possible. There are no limitations on what you can display in the master and detail views of a split view controller.
Think of the split view as merely a view that displays two of your view controllers. They can contain anything.
If I understood what you're trying to achieve correctly then what you want to do is create a View Controller with a UITableView which will display the data in the left column. This View Controller should also have a UINavigationController which will push another view controller when you select something in the table.
So far, so good. Nothing has changed in the detail view since it in reality isn't aware of or connected to the master view in any way. When the user has moved enough steps down in the master view, and you want to change the detail view - then you can set the viewControllers-property of the navigation controller to update the detail view with whichever view controller you want to display.
Yes you can. Apple has provided a sample code called MultipleDetailViews
It shows how communication is done between master and detail using delegation.
See these Tutorials :-
http://www.icodeblog.com/2010/04/05/ipad-programming-tutorial-hello-world/
http://www.raywenderlich.com/1040/ipad-for-iphone-developers-101-uisplitview-tutorial
These will help you.

TabBarController inside NavigationController

Imagine that we have multiview apllication which is controlled by Navigation Controller. We go from the first view to second by using pushViewController method and that's not a problem but then we need to move to the third view. And the third one is a view which looks like a TabBar. How do we do that? The third view is supposed to be controlled by TabBarController, isn't it?
So how to pass the control? I declared an outlet UITabBarController * tbc and connected it to TabBarController in xib file and then i tried this in viewDidLoad:
tbc = [[UITabBarController alloc]init];
and it shows nothing.
Your help is highly appreciated
It's a bit wierd. Its more standard to have a tabBarController that switches views and some of those views may be navigation controllers. But ...
Create the UITabBarController and push it.
NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
// create someView
[viewControllers addObject:someView];
// create someView2
[viewControllers addObject:someView2];
UITabBarController *tabController = [[UITabBarController alloc] init];
[tabController setViewControllers:viewControllers];
[[self navigationController] pushViewController:tabController animated:YES];
Then, from the tabBarContoller view, based on some action, you can choose to pop it:
[self.navigationController popViewControllerAnimated: NO];
You can wire it up in the storyboard editor in the latest version of Xcode.
However, since this is very much non-standard use of the controls, you would need a very good reason as to why you would want a UI like this.
And even then, Apple's review process might turn your app down if the interface is clunky.