Show full image from time to time - iphone - iphone

I want from time to time (after the user does 3 steps ) to show an image .
I have a class for Image that contains an Uiimageview and diffrent methods (if i click opens an url etc )
I have the first class that contains an UiWindow and when i acces image i do window addSubview:myimageclass.view....and this is working
but I have more other classes that don t have window but tableview or mapview....I don t know how to add code in this classes . I shell show the image with add just from time to time ...how can i display it ?
i know android programming and on ios I am new , in android i put the image in separate activity and i was calling this and i set a sting and after i was returning to activity from string .
Thanks a lot ,
Raluca

You could keep your UIImageView as a property of appDelegate. Let's call it
imageView.
In didFinishLaunchingWithOptions: hide this imageView and add it to appDelegate.window like so:
[self.imageView setHidden:YES];
[self.window addSubview:imageView];
You can then define two methods of appDelegate:
-(void) showImage {
[self.imageView setHidden:NO];
[self.window bringSubviewToFront:self.imageView];
}
-(void) hideImage {
[self.imageView setHidden: YES];
}
and use this two methods when needed

Related

UIPopoverController for iphone not working?

I need to use a UIPopOverController for my iPhone app ,i searched stackoverflow someone said UIPopoverController does not run on iphone iphone device WHY?.when i run on iphone device
i got this error reason: '-[UIPopoverController initWithContentViewController:]
called when not running under UIUserInterfaceIdiomPad.'
-(void)btnSetRemainderTapped:(UIButton *)button
{
setReminderView =[[SetRemainderView alloc]initWithNibName:#"SetRemainderView" bundle:[NSBundle mainBundle]];
setReminderView.contentSizeForViewInPopover = CGSizeMake(setReminderView.view.frame.size.width, setReminderView.view.frame.size.height);
setReminderView.delegate = self;
popOverController = [[UIPopoverController alloc]
initWithContentViewController:setReminderView] ;
CGRect rect = CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/2, 1, 1);
[popOverController presentPopoverFromRect:rect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
can any one help me?
You CAN use popoverController in iPhone apps.
1. Create a category
// UIPopoverController+iPhone.h file
#interface UIPopoverController (iPhone)
+ (BOOL)_popoversDisabled;
#end
// UIPopoverController+iPhone.m file
#implementation UIPopoverController (iPhone)
+ (BOOL)_popoversDisabled {
return NO;
}
#end
2. Import it to your class and use popover in iPhone as usual.
But remember that this is private method and Apple can reject your app. But I know people who use this normally and Apple published their apps.
Edit: As stated by Soberman, since iOS 8 it is possible to present popovers on iPhone using public APIs, so this answer is probably not relevant anymore.
As stated in Apple's documentation on UIPopoverController:
Popover controllers are for use exclusively on iPad devices.
So there is no way to use this class in iPhone application unfortunately. But there are a couple of custom third-party implementations of the functionality provided by UIPopoverController which add iPhone support and more. See https://github.com/50pixels/FPPopover for example.
Edit: There also is another highly customizable popover implementation for both iPhone/iPad worth checking out: https://github.com/nicolaschengdev/WYPopoverController.
Since iOS8 we are now able to create popovers, that will be the same on iPhone, as on iPad, which would be especially awesome for those who make universal apps, thus no need to make separate views or code.
You can get the class as well as demo project here: https://github.com/soberman/ARSPopover
All you need to do is subclass UIViewController, conform to the UIPopoverPresentationControllerDelegate protocol and set desired modalPresentationStyle along with the delegate value:
// This is your CustomPopoverController.m
#interface CustomPopoverController () <UIPopoverPresentationControllerDelegate>
#end
#implementation CustomPopoverController.m
- (instancetype)init {
if (self = [super init]) {
self.modalPresentationStyle = UIModalPresentationPopover;
self.popoverPresentationController.delegate = self;
}
return self;
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone; //You have to specify this particular value in order to make it work on iPhone.
}
Afterwards, instantiate your newly created subclass in the method from which you want to show it and assign two more values to sourceView and sourceRect. It looks like this:
CustomPopoverController *popoverController = [[CustomPopoverController alloc] init];
popoverController.popoverPresentationController.sourceView = sourceView; //The view containing the anchor rectangle for the popover.
popoverController.popoverPresentationController.sourceRect = CGRectMake(384, 40, 0, 0); //The rectangle in the specified view in which to anchor the popover.
[self presentViewController:popoverController animated:YES completion:nil];
And there you have it, nice, neat blurred popover.
So #Sobermans answer didn't really solve the issue from start to finish for me so I want to detail how I got it done using the docs. That being said I do like the idea of using your own presentation controller subclass to manage all of the customisation you want to exhibit.
1. Create your controller to present
The first step is instantiating the controller you want to present:
let vc: UIViewController = ...
vc.modalPresentationStyle = .Popover
vc.preferredContentSize = CGSize(width: CGRectGetWidth(view.bounds)/2, height: 100)
Now we have a controller with the popover presentation style and an arbitrary content size.
2. Implement adaptivePresentationStyleForPresentationController
By default UIPopoverPresentationController will present on full screen on iPhone so to prevent this behaviour you need to force the adaptive presentation style to none.
First we set the delegate of the popover presentation controller
vc.popoverPresentationController.delegate = self;
Then we implement UIPopoverPresentationControllerDelegate
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return .None;
}
3. Present and configure popup
First we need to call presentViewController and only after that can we configure the popover:
presentViewController(vc, animated:true, completion:nil)
if let popover = vc.popoverPresentationController {
popover.permittedArrowDirections = .Right | .Left
popover.sourceView = button
popover.sourceRect = button.bounds
}
Use a custom popover controller, such as:
https://github.com/sammcewan/WYPopoverController
(this seems to be the best supported one that I have found).
I ended up creating my custom tooltip/popover class.
Can be initalised with any content view and dynamically adjusts it's frame.
Hope it helps.
https://github.com/akeara/AKETooltip
If you want to do it in Swift, I believe the code is the following:
extension UIPopoverController {
class var _popoversDisabled : Bool {
get { return false }
}
}
Edit: It is working in Xcode 6 beta 4 on iPhone with iOs7.1
This is a really interesting (and depressing) thread to read. I can't believe Apple prevents popup dialogs on iPhones, with absolutely no justification.
And, it's true, on iOS 8, if you try to work around this limitation, it'll make your popups appear as a full-screen modal dialog.
The following excellent webpage describes "How Apple Cheats" to let its own iBooks and iTunes apps break its own rules, and allow popups - but just from within their own iPhone apps.
HowAppleCheats
Have a read (warning: it'll make you hate Apple & XCode even more..)
Want to get around the "UIPopoverController called when not running under UIUserInterfaceIdiomPad" error on iOS 8 ?
Simple.
Just go into your .plist file, and change the Bundle ID to "com.apple.itunesu" to make XCode think that your app is actually iTunes.
Then your popup will work fine.
(Sigh.)
The alternative way of doing this is to directly add your UIViewController to your screen.
In this example, I wanted a "helper screen" to appear on top of my iPhone screen. It's a UIViewController, it is stored in it's own .xib file, and it has a few lines to add a pretty border:
- (void)viewDidLoad {
[super viewDidLoad];
// Give our popup a pretty curved border
self.view.layer.borderColor = [[UIColor blueColor] CGColor];
self.view.layer.borderWidth = 1.0;
self.view.layer.cornerRadius = 8;
}
To display it, I simply create an instance of this UIViewController, add it to my screen, then center it:
-(void)showHelperScreen
{
if (self.helperScreen == nil)
{
// Add the popup UIViewController to our screen
self.helperScreen = [[HelperViewController alloc] init];
[self.view addSubview:self.helperScreen.view];
}
// Center the popup in the middle of the screen
CGSize screenSize = [[UIScreen mainScreen] applicationFrame].size;
self.helperScreen.view.center = CGPointMake(screenSize.width/2, screenSize.height/2);
}
Of course, I also needed to add some code to make the popup disappear when the user taps outside of it, but this does at least show that you can (safely) display popups on an iPhone, even if your app isn't specifically called iTunes or iBook.
Voila.
Hope this helps, and if anyone needs me, I'll be back in my safe, happy place (Visual Studio, in other words).

Creating an UIImageView on UIButton click

I have a button setup in one class and on clicking it, I am calling a method which is in another class and in that method I have setup code for UIImageView.
the method is getting called, But somehow the ImageView doesnt seems to get created, I dont understand why, And therefore I cannot add any Image to that ImageView.
Here's some code of what I am trying to do:
In my first viewControllerClass I have this method which fires on a button click:
- (IBAction)buttonClicked:(id)sender {
[secondViewController createNewImageView];
}
and then in the second viewController I am calling that createNewImageView method, in which I am creating an ImageView at runtime:
-(void)createNewImageView {
newImageView = [[UIImageView alloc] initWithFrame:CGRectMake(100,100, 100, 100)];
newImageView.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.4];
[self.otherNewView addSubview:newImageView];
}
But somehow the ImageView is not getting added on my view, Any Ideas why this is happening ?
What am i missing here ..?
Thanks for your time :)
EDIT : The class which holds the button is UIPopOverController.
Have you allocated secondViewController before doing,
[secondViewController createNewImageView];
I think you have not created an object for secondViewController.
So before calling the function create the object for the class.

UIPickerView on Separate Page

I am working on an iPhone app. Initially, I had my pickerview in the same screen so this was just a one page app. After skinning it i realized that i want the pickerview on it's own separate page. So i did that. However, my pickerview originally would update uilabels and other objects on that same page. How can I have my pickerview access those objects from it's new view?
- (IBAction)ShowPickerAction:(id)sender {
if (self.theView == nil) {
theView = [containerView initWithNibName:#"containerView" bundle:nil];
theView.parentView = self;
}
self.theView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:self.theView animated:YES];
}
That is the method I am using to call my new view. But line 3 of the above code gives me the error "No known class name for selector initWithNibName:bundle". I think that error is related to something that i did wrong in my header file. My new class is called containerView. So i did this in my header:
#interface ViewController : UIViewController {
containerView *theView;
But that gives me the error "Unknown type name containerView" even though i do have a class named containerView!!
Look into uiappdelegate protocol or try passing values to through a static function to the previous page.
Use a delegate to pass information back and forth to the view object that instantiatrd the picker view. You want to keep your code coupling as loose as possible, especially if you might like to drop it into your next project. Using a delegate and/or blocks are some of the best ways.

UIWebView without Copy/Paste and selection rectangle when showing documents

In my App I want to disable the Copy/Paste/Cut of contents that are showed by a UIWebView object. To achieve this, I created a UIWebView subclass and overrode the - (BOOL)canPerformAction:(SEL)action withSender:(id)sender method:
#pragma mark - UIResponderStandardEditActions
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == #selector(copy:) ||
action == #selector(paste:)||
action == #selector(cut:)) {
return _copyCutAndPasteEnabled;
}
return [super canPerformAction:action withSender:sender];
}
Now the user no longer can make such operations, however the UIWebView still shows the "selection rectangle", as you can see in the following screenshot:
NOTE: The content being showed in the UIWebView is not HTML pages. I'm showing document files (PDF, DOC, PPT), loaded from a file using:
NSURL *fileURL = [NSURL fileURLWithPath:<document file path..>];
NSURLRequest *fileRequest = [NSURLRequest requestWithURL:fileURL];
[<uiwebView> loadRequest:fileRequest];
Is there any way to disable/hide this selection rectangle feature too?
[]s,
You can try injecting javascript into the webView. This code works on the iPhone too but only when the page is fully loaded.
http://www.javascriptsource.com/page-details/disable-text-selection.html or
http://solidlystated.com/scripting/proper-way-to-disable-text-selection-and-highlighting/
To get it to work properly when the page is only half loaded or still loading you'll proably have to use a setup similar to this one where you inject the disabling javascript just as it would start selecting.
http://www.icab.de/blog/2010/07/11/customize-the-contextual-menu-of-uiwebview/
One last thing I can think of that might work, have you tested adding the selector #selector(select:) to the list of disabled selectors?
EDIT:
Ok as you what to display a PDF rather then a html page you can try this.
Setup
Put the webView in a scrollView;
Set the ScrollView's: delegate to self; min zoom to 1.0 and max zoom to whatever you want;
After the weView has finished loading.
Scan for the first UIScrollView in the webView, (we are scanning for it so this shouldn't break on later iOS versions).
Set the UIWebViews frame to it's scrollViews size.
And set the scrollView's contentSize to the webViews contentSize (or by now, it's frame).
-(void)setup{
//Get the webView's first scrollView (the one all the content is in).
UIScrollView *webViewContentView;
for (UIView *checkView in [webView subviews] ) {
if ([checkView isKindOfClass:[UIScrollView class]]) {
webViewContentView = (UIScrollView*)checkView;
break;
}
}
webView.frame = CGRectMake(0, 0, webViewContentView.contentSize.width, webViewContentView.contentSize.height);
scrollView.contentSize = webViewContentView.contentSize;
scrollView.delegate = self;
}
To enable zooming you'll need to add an extra method from the scrollViews delegate.
-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
return webView;
}
And you can download a sample project http://www.multiupload.com/P8SOZ4NW6C (It's an xcode 4 project).
NOTES: The zooming pixelates the webView's content if you zoom in to much because the webView doesn't know to re-render things, and of cause you can't click links (but in a PDF that shouldn't be a problem).
EDIT 2: Better method
Since writing this I have realised a MUCH simpler and easier method, which should also solve the pixelation problem.
-(void)removeInput{
UIScrollView *webViewContentView;
for (UIView *checkView in [webView subviews] ) {
if ([checkView isKindOfClass:[UIScrollView class]]) {
webViewContentView = (UIScrollView*)checkView;
break;
}
}
for (UIView *checkView in [webViewContentView subviews] ) {
checkView.userInteractionEnabled = NO;
}
}
Now you should only be able to interact with the UIScrollView itself, meaning zooming, scrolling, ect.. should still work (and the page should re-render rather the pixelating when you zoom) but you can't select any text, or type anything in.
Also something about this method is that based on this list of UIWebView subViews it might be possible to set this before the page starts loading rather then after it has finished loading the page.
I found an answer on here by Johnny Rockex and it worked like a champ. It works for documents (including .pdf files) displayed in a UIWebView. UIWebView without Copy/Paste when displaying PDF files

iPhone Programming Random Views When I tap button

I have 5 different views and when I tap the button, I want to push one of 5 views randomly. However, I do not want to make 5 different controllers for each views. Do I have a chance to put them in one controller? If so, how?
You can have as many views as you want in a single UIViewController subclass. You can create them all in Interface Builder as well, in the one .xib file for your UIViewController class (it might get a bit hard to see, though, better to lay out each UIView in its own .xib). You can present them in any combination you want. Assuming you want to just show one view at a time, you can do this:
In your viewDidLoad method, start out showing the initial view, and in your class keep track of which is the current view:
...
#property(nonatomic, retain) UIView *currentView;
...
- (void)viewDidLoad
{
[self.view addSubview:self.defaultView];
self.currentView = self.defaultView;
}
Then to switch to a particular other view do this:
- (void) switchToView:(UIView *)newView
{
[self.currentView removeFromSuperView];
[self.view addSubview:newView];
self.currentView = newView;
}
}
Or you can show them all at one time: just [self.view addSubview:theView];
You have to set the tags of all the views, and then use
int r = arc4random() % 5;
method to find the random number. Use this newly generated number to check the tag, in your selector
Just change the view outlet of the controller on the action of the button. Use random numbers to select which view. Ouh and - (void)setNeedsDisplay :)