ViewController as Subview - iphone

I have one UIViewController (DownloadManager) which downloads a webpage with UIWebView and display the downloaded content (say a pdf file). I want to use it as reusable component.
I am having another UIViewController pointing to a screen which contains 4 buttons each button should download and display one pdf file from webpage. I want to display the downloaded pdf as a embedded subview to the current screen. Basically don't want navigation.
Here I tried to use DownloadManager as subview and display pdf as subview. Its working fine.
I read as Per screen one View Controller. But still can I use this approach ?
I have to send a email feedback from this screen. As soon as I present the MFMailComposeViewController the previous download manager subview disappears Technically what is happening here?
Please find the code snippet below:
//************************************************
#implement MyViewController
- (void) initWebView
{
downloadMgr = [[DownLoadViewController alloc] initWithNibName:#"DownLoadViewController" bundle:nil];
downloadMgr.view.frame = CGRectMake(140, 20, 300, 200);
downloadMgr.WebView.frame = CGRectMake(140, 20, 300, 200);
downloadMgr.view.backgroundColor = [UIColor whiteColor];
downloadMgr.WebView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:downloadMgr.view];
[self.view addSubview:downloadMgr.WebView];
}
----
----
- (IBAction)onHomeClick:(UIButton *)sender
{
switch (sender.tag)
{
case 101:
{
[self stopWebViewResources];
if (!downloadMgr) {
[self initWebView];
}
downloadMgr.NavigationURL = [[NSURL alloc] initWithString:#"https://xxx.yyyyyy.com/sites/pex/iPadFiles/abc.pdf"];
downloadMgr.title = #"ABC";
[downloadMgr LoadURL];
}
break;
case 120:
{
//Send feedback via email
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
----
----
mail.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:mail animated:YES];
}
}
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissModalViewControllerAnimated:YES];
}
#end
//*****************************************************************
#interface DownLoadViewController : UIViewController <MyWebViewDelegate>{
NSURL* NavigationURL;
IBOutlet myWebView* WebView;
IBOutlet UIActivityIndicatorView* ProgressView;
NSString* DownloadedFileName;
}

It is much more robust to separate the download functionality and the display into two different classes.
Ideally, you should have a class like "Downloader" or "Download Manager" which is a subclass of NSObject. This class should do the download and notify its delegate when it is done. Then you can have as many UIViewControllers as you like that take advantage of your Downloader class.

Related

Custom view for taking image though camera in ios 7

//---------- viewController.h file has
IBOutlet UIView *viewCamera; // custom view for taking camera reason
//------------viewController.m :: inside viewDidLoad method
// -- camera start
[viewCamera setBackgroundColor:[UIColor clearColor]];
imgPicker = [UIImagePickerController new];
imgPicker.allowsEditing = YES;
imgPicker.delegate = self;
imgPicker.cameraOverlayView = viewCamera;
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imgPicker animated:NO completion:nil];
// ----- ob btn clicked
- (IBAction)btnTakePhotoPressed:(id)sender{
[imgPicker takePicture];
}
App is crashed due to this line, if I comment this line code is running but camera is open in full view, I want to add my camera access on my custom view.
imgPicker.cameraOverlayView = viewCamera;
Thanks in advance :)

Adding view onto Window of Twitter

Hi i am facing a strange problem. I have a class which is NSObject type, and on same class i want to share image on Facebook and Email and also on Twitter. I successfully done FB and Email, On Email i use [[[UIApplication sharedApplication] keyWindow] addSubview:mailer.view]; to show Mail controller onto Window instread of Presenting Model view controller and same as on Dismiss i use Remove from super view. But when i do same with twitter controller then twitter controller just show me few mili seconds and then hide. I also add new View controller onto Window and then Present its controller onto that View controller but same output. Don't know what going wrong. Please help on that. Thanks in advance. This will be great for me :)
Edited
if (_engine != nil) {
_engine = nil;
[_engine release];
}
_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate: self];
_engine.consumerKey = kOAuthConsumerKey;
_engine.consumerSecret = kOAuthConsumerSecret;
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate: self];
if (controller) {
NSLog(#"Sharing on Twitter on loading controller of twitter");
[[UIApplication sharedApplication].keyWindow addSubview:controller.view];
}
else {
NSLog(#"Sharing on Twitter in else condition");
[self sharetoTwitter:screenImg];
}
ShareToTwitterMethod
- (void) sharetoTwitter:(UIImage *)img {
NSString *response = [_engine _uploadImage:img requestType:MGTwitterPublicTimelineRequest responseType:MGTwitterStatus];
NSLog(#"twitter post notification");
}
I used Add subview to load view instead of Presenting View.
Have you tried this
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
[window addSubView:controller.view];
[window makeKeyAndVisible];//important line dont forget to set this line

Access/Reference imageView.image to replace it later

Alright, this has been driving me insane for the last few days. I hope this is an easy fix. I am trying to access my UIImageView.image so I can replace it later.
With only the needed code here it is:
I first create an imageView for each time a user clicks on a photo in their Library.
// .h
#property (strong,nonatomic) UIImage *doneImage;
#property (strong,nonatomic) UIImageView *editImage;
// .m
#synthesize doneImage;
#synthesize editImage;
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(xCoordinate, yCoordinate, widthOfImage, heightOfImage)];
imgView.image = originalImage;
imgView.userInteractionEnabled = YES;
[self.mainView addSubview:imgView];
// I add gestures
longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(handleLongPress:)];
longPress.minimumPressDuration = 1;
longPress.numberOfTouchesRequired = 1;
[imgView addGestureRecognizer:longPress];
}
When someone clicks holds down on one of those images this pops up
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {
editImage = (UIImageView*)recognizer.view; // I declare what editImage is and now have access to what imageView is being interacted with.
doneImage = editImage.image; // I then get the image of that for later use.
if(UIGestureRecognizerStateBegan == recognizer.state) {
popoverEditor = [[UIPopoverController alloc] initWithContentViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"popupEditor"]];
[popoverEditor presentPopoverFromRect:editImage.bounds inView:editImage permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
This pops up a menu with 4 buttons. I then have an IBAction that tries to fire up the editing part.
- (IBAction)effectsEditorButton:(id)sender {
// editImage is my imageView
if (editImage == nil) {
NSLog(#"imageView was nil wtf");
} else {
[self performSelector:#selector(editImageWithEffects:)];
}
}
My image view is ALWAYS nil. I need to get a reference to that somehow. I've tried tags but I can't seem to get them to hold their value once I get to this point.
Any help would be very much Appreciated. Thanks!
Are you leaving out code? Based on what you have here, editImage will always be null because it isn't declared anywhere (perhaps something in Interface Builder that we can't see?).
You need a way to link back into your view controller from your popover. Popular ways to do this are to use a completion block when returning from your popover controller, or setting your "main" VC as a weak property on the popover.

Presenting a modal view from another class (MFMailComposeViewController)

I am trying to create a class who will be responsible to send emails using MFMailComposeViewController so i can use this methods from differences views controls in my app.
This class is called apoio.
In this class have the method bellow.
-(void) enviarGraficoPorEmail: (NSData*) _pdfGrafico {
if (![MFMailComposeViewController canSendMail]) {
// show message box for user that SMS cannot be sent
} else {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Dashboard"];
[picker addAttachmentData:_pdfGrafico mimeType:#"application/pdf" fileName:#"grafico.pdf"];
NSString *emailBody = #"Anexando gráfico";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
}
}
I have another view controller who calls the apoio method when the user clicks on email button. It is this code bellow
-(IBAction) enviarGraficoPorEmail {
Apoio *apoio = [[Apoio alloc] init];
[apoio enviarGraficoPorEmail:[barChart dataForPDFRepresentationOfLayer]];
}
But i don't know why, the email view doesn't appear. The method is called correct because i debuged and so on.
If i copy the code from apoio method to enviarGraficoPorEmail method, everything works perfect.
But i don't want to do this, beucase ill send emails from others views controller.
What am i doing wrong ??
You could do it a couple of different ways.
Option 1: Pass the calling view controller as a parameter to the class method
-(IBAction) enviarGraficoPorEmail {
Apoio *apoio = [[Apoio alloc] init];
[apoio enviarGraficoPorEmail:[barChart dataForPDFRepresentationOfLayer] callingController:self];
}
-(void) enviarGraficoPorEmail: (NSData*) _pdfGrafico callingController:(UIViewController*)_callingController {
...
[_callingController presentModalViewController:picker animated:YES];
...
}
Option 2: Add a class variable for the calling view controller
-(IBAction) enviarGraficoPorEmail {
Apoio *apoio = [[Apoio alloc] init];
apoio.callingController = self;
[apoio enviarGraficoPorEmail:[barChart dataForPDFRepresentationOfLayer]];
}
-(void) enviarGraficoPorEmail: (NSData*) _pdfGrafico callingController:(UIViewController*)_callingController {
...
[callingController presentModalViewController:picker animated:YES];
...
}
Then you'd add callingController to your class as a retain property, initialize it to nil, and release it in dealloc.
Option #1 is probably the better approach for your needs.
Thanks a lot!. It is now working but i still have one problem.
I have the method on my generic class who is supposed to be responsible to hide the mailController.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
break;
case MFMailComposeResultSent:
// FAILS
[self.parentViewController dismissModalViewControllerAnimated:YES];
break;
case MFMailComposeResultFailed:
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
In the method that creates the mailController have the property
picker.mailComposeDelegate = self;
I tried to change to
picker.mailComposeDelegate = _callingController.self;
I set the MFMailComposeViewControllerDelegate on my generic class already.
But it only works when i copy the method didFinishWithResult and put it on the origin controller, what is not my intention, because i want to put all this code on a generic class.
What am i doing wrong ??
Ok, this is an answer to your second question (which you posted as an answer to your first question).
Here's how I would set it all up:
In your calling view controller .h file:
#interface MyViewController : UIViewController <MyMailDelegate> {
Apoio *apoio;
}
In your calling view controller .m file:
-(IBAction) enviarGraficoPorEmail {
apoio = [[Apoio alloc] init];
apoio.callingController = self;
[apoio enviarGraficoPorEmail:[barChart dataForPDFRepresentationOfLayer]];
}
-(void) enviarCompleto {
//do whatever here after send email completes
[apoio release];
}
In your Apoio .h file
#protocol MyMailDelegate
#required
-(void) enviarCompleto;
#end
#interface OfferObject : NSObject {
UIViewController <MyMailDelegate> *callingController;
}
#property (nonatomic, retain) UIViewController <MyMailDelegate> *callingController;
In your Apoio .m file
-(void) enviarGraficoPorEmail: (NSData*) _pdfGrafico callingController:(UIViewController*)_callingController {
...
[callingController presentModalViewController:picker animated:YES];
...
}
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
...
}
[callingController dismissModalViewControllerAnimated:YES];
[callingController enviarCompleto];
}
Then don't forget to do this on init:
callingController = nil;
And on dealloc:
[callingController release];
Also, don't forget your most important step: up vote both my answers :)
You need to call [self presentModalViewController:picker animated:YES];
from a UIViewController class then it will show you email composer, you can do this, whenever you call from some view controller you can pass its reference and you can change the above line as this
[callingController presentModalViewController:picker animated:YES];

UIImage VIew Not loading image?

I have a UIImage on my MyCartViewController. And i have a button on my BlackFacePlatesViewController. I want this button to set the image on the MyCartViewController to another image, however i can't seem to get it to work. Here is my code for the button to set the image on the MyCartViewController:
EDIT:
I HAVE 1 BOOL VARIABLE IN VC2 AND WHEN THE BUTTON IS PRESSED IN VC1, I SET THE BOOL AND CREATE VC2. THEN IN THE VIEWWILLAPPEAR METHOD, I SET THE IMAGE ACCORDINGLY. HERE IS THE CODE: THANKS
- (IBAction)outlet1 {
MyCartViewController * imageCart = [[MyCartViewController alloc]init];
imageCart.displayImage = YES;
}
BUTTON THAT THE USER PRESSES ^
THIS IS TO SWITCH PAGES:
- (IBAction)myCart:(id)sender; {
MyCartViewController * first = [[MyCartViewController alloc]init];
first.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:first animated:YES];
[first release];
}
THIS IS THE VIEWWILLAPPEAR METHOD IN VC2:
-(void)viewWillAppear:(BOOL)animated{
if (self.displayImage == YES) {
UIImage * myImage = [UIImage imageNamed:#"Paddle_3.png"];
[outletImageView setImage:myImage];
}
}
WOULD THIS WORK? THANK YOU VERY MUCH FOR THE HELP EVERYBODY!!!
You aren't allocating your object cart properly, and cart2 has nothing to do with cart, apart from being a similar class. You should just make an instance variable in the header file that's a UIImage which holds the image you want to use in your next UIView, and set it when you use presentModalViewController: Try this. In your .h file,
#interface MyCartViewController : UIViewController {
UIImage * imageForNextView;
}
In your .m file,
- (IBAction)outlet1:(id)sender; {
imageForNextView = [UIImage imageNamed:#"Paddle_1.png"];
}
- (IBAction)outlet2:(id)sender; {
imageForNextView = [UIImage imageNamed:#"Paddle_2.png"];
}
- (IBAction)myCart:(id)sender; {
MyCartViewController * cart2 = [[MyCartViewController alloc]init];
cart2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
cart2.newImage.image = imageForNextView;
[self presentModalViewController:cart2 animated:YES];
[cart2 release];
}
I made 2 button methods to illustrate the point of switching the image based on which button was pushed. Remember that imageNamed: caches the UIImage, so if you have a lot, you may have to start looking into other loading methods, all of which can be found here. This should solve your problem.
Edit: Try this. In your .h file,
#interface MyCartViewController : UIViewController {
NSString * nameOfImage;
UIImageView * imageView;
}
#property (nonatomic, retain) NSString * nameOfImage;
In the .m file,
#synthesize nameOfImage;
- (void)viewDidLoad; {
[super viewDidLoad];
imageView.image = [UIImage imageNamed:nameOfImage];
}
Then, when you push to the view, try:
- (IBAction)myCart:(id)sender; {
MyCartViewController * cart2 = [[MyCartViewController alloc]init];
cart2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
cart2.nameOfImage = #"Paddle_2.png";
[self presentModalViewController:cart2 animated:YES];
[cart2 release];
}
Edit2: This is a tutorial about how to use a UISplitViewController, which has a similar idea, where you press a UITableViewCell, and it changes an image in another UIView. You should look through this, and see if it can help your project.
Also, try:
- (IBAction)myCart:(id)sender; {
MyCartViewController * cart2 = [[MyCartViewController alloc]init];
cart2.nameOfImage = #"Paddle_2.png";
[self pushViewController:cart2 animated:YES];
[cart2 release];
}
This is another way to move to a new UIView, which may work.
Hope that Helps!
Are you sure this line of code is actually executed? Did you check that while debugging or with an NSLog?
If not then check the reference to the action 'outlet1'.
What is cart?
What is cart.newimage?
How are those linked to the UIImagaeView that is part of 'the next page'?
You may need to force the redraw via setNeedsDisplay.
Assuming your cart.newImage is the UIImageView object:
[cart.newimage setNeedsDisplay]; //might do the trick.
However, that is a bit view of code to really understand the issue.