iOS PopOver with Webview - ios

I am trying to build a pop over that displays a webView. So far, I have a button that when pressed, generates a pop over but the web view does not display. Using NSlog, I can see that the viewDidLoad method is firing. Here is the code I have so far:
In the popover.h file:
#interface PopUpViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *liveTimingPopUp;
#end
In the popover.m file:
#import "PopUpViewController.h"
#interface PopUpViewController ()
#end
#implementation PopUpViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadWebView];
}
- (void) loadWebView
{
// Do any additional setup after loading the view.
NSString *fullURL = #"www.google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_liveTimingPopUp loadRequest:requestObj];
[self loadView];
NSLog(#"PopUpView Fired");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
In the control view containing the button that generates the popover .h file:
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController
{
UIInterfaceOrientation intOr;
// APICallsViewController *pendingApiCallsController;
}
#property (strong, nonatomic) IBOutlet UIWebView *liveVideoPage;
- (IBAction)liveTimingButton:(id)sender;
#property (strong, retain) UIPopoverController *popOver; //declare UIPopOver so that you have an object to work with
-(void) loadVideo;
#end
In the control view containing the button that generates the popover .m file:
#import "SecondViewController.h"
#import "PopUpViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self loadVideo];
}
- (void) loadVideo
{
NSString *fullURL = #"http://192.168.130.230:1935/live/camera.stream/playlist.m3u8";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_liveVideoPage loadRequest:requestObj];
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
intOr = self.interfaceOrientation;
if (intOr == UIInterfaceOrientationPortrait)
{
NSLog(#"portrait");
_liveVideoPage.frame = CGRectMake(10, 100, 600, 550);
}
else
{
NSLog(#"landscape");
_liveVideoPage.frame = CGRectMake(200, 100, 600, 550);
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)liveTimingButton:(id)sender
{
if ([_popOver isPopoverVisible]) {
[_popOver dismissPopoverAnimated:YES];
}
else
{
PopUpViewController *PUVC = [[PopUpViewController alloc]init];//instantiate ViewController for popOver's content
_popOver = [[UIPopoverController alloc] /* create Popover*/initWithContentViewController:PUVC]; //fills popover with PopUpViewController
_popOver.popoverContentSize = CGSizeMake(800, 250);
[_popOver presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; //specifies where the popover is coming from...the liveTiming Bar button
}
}
#end
Any help would be greatly appreciated!

where is the webview supposed to come from? The VC gets not passed a xib name.. so the outlet will never bet set

Related

launch a webview programmatically from another view controller error

Here is my main view controller:
#import "ViewController.h"
#import "WebViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
WebViewController *wvc = [[WebViewController alloc]init];
[self presentViewController:wvc animated:NO completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Here is webviewcontroller:
#import "WebViewController.h"
#interface WebViewController ()
#end
#implementation WebViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024,768)];
NSString *url=#"https://www.google.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
[self.view addSubview:webview];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
The google page does not get loaded and the warning I get is
Warning: Attempt to present on
whose view is not in the window
hierarchy!
What the hell is going on?
I'd suggest double-checking. Following your code, I just confirmed this:
#import "LoadPresentViewController.h"
#import "WebViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)doLoadPresent:(id)sender {
WebViewController *wvc = [[WebViewController alloc]init];
[self presentViewController:wvc animated:NO completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// this fails in viewDidLoad
// WebViewController *wvc = [[WebViewController alloc]init];
// [self presentViewController:wvc animated:NO completion:nil];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// this succeeds in viewDidAppear
WebViewController *wvc = [[WebViewController alloc]init];
[self presentViewController:wvc animated:NO completion:nil];
}
#end

Why the String value is null?

Why the string value is getting null value. Someone please find the error?
How can i fetch the string value when the method is getting called from SecondViewController do i need a another string or someone please suggest some corrections.
I need this string to be have some value
#property (nonatomic,strong) NSString *str;
When the following method is getting called.
[view getTotal];
Label is in the first view controller.
Thanks in advance.
In ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
#interface ViewController ()
{
NSInteger total;
}
#property (weak, nonatomic) IBOutlet UILabel *lbl;
#property (weak, nonatomic) IBOutlet UIButton *btn;
#property (nonatomic,strong) NSString *str;
- (id)initWithInitialNumber:(NSInteger)initialNumber;
- (void)addNumber:(NSInteger)newNumber;
- (NSInteger)getTotal;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"%#",_str);
self.lbl.text = _str;
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSLog(#"viewDidAppear called");
[self viewDidLoad];
}
-(id)initWithInitialNumber:(NSInteger)initialNumber{
total = initialNumber;
return self;
}
-(void)addNumber:(NSInteger)newNumber{
total += newNumber;
}
-(NSInteger)getTotal{
NSLog(#"Number is: %ld",total);
self.str = [NSString stringWithFormat:#"%ld",total];
NSLog(#"%#",self.str);
return total;
}
- (IBAction)btnAct:(id)sender {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SecondViewController *SC = [sb instantiateViewControllerWithIdentifier:#"SC"];
[self presentViewController:SC animated:YES completion:nil];
}
-(NSString *)str{
return _str;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
In SecondViewController.m
#import "SecondViewController.h"
#import "ViewController.h"
#interface SecondViewController ()<Sendata>
{
NSInteger *num;
}
#end
#implementation SecondViewController
- (IBAction)backBtn:(id)sender {
ViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:#"VC"];
[self presentViewController:VC animated:YES completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
ViewController *view = [[ViewController alloc]initWithInitialNumber:10];
view.delegate = self;
[self Print:#"Hello"];
[view addNumber:2];
[view getTotal];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)Print:(NSString *)str{
NSLog(#"%#",str);
}
#end
What I can understand from your code is that you are presenting secondViewController from ViewController on a button click. On Second view controller you want to add two numbers and show them on Firstview controller again.
what I can see from your code , you are pushing the new instance of ViewController every time. So thevalue you are intialising in your view didLoad in diffrent object then the one your pushing again on button action of back. IF you can modify you code like this , it will work :
#interface ViewController ()
{
NSInteger total;
}
#property (weak, nonatomic) IBOutlet UILabel *lbl;
#property (weak, nonatomic) IBOutlet UIButton *btn;
#property (nonatomic,strong) NSString *str;
- (id)initWithInitialNumber:(NSInteger)initialNumber;
- (void)addNumber:(NSInteger)newNumber;
- (NSInteger)getTotal;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"%#",_str);
self.lbl.text = _str;
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSLog(#"viewDidAppear called");
[self viewDidLoad];
}
-(id)initWithInitialNumber:(NSInteger)initialNumber{
total = initialNumber;
return self;
}
-(void)addNumber:(NSInteger)newNumber{
total += newNumber;
}
-(NSInteger)getTotal{
NSLog(#"Number is: %ld",total);
self.str = [NSString stringWithFormat:#"%ld",total];
NSLog(#"%#",self.str);
return total;
}
- (IBAction)btnAct:(id)sender {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SecondViewController *SC = [sb instantiateViewControllerWithIdentifier:#"SC"];
[self.navigationController pushViewController:SC animated:YES];
}
-(NSString *)str{
return _str;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
#interface SecondViewController ()
{
NSInteger *num;
ViewController *viewController;
}
#end
#implementation SecondViewController
- (IBAction)backBtn:(id)sender {
[self.navigationController popViewControllerAnimated:YES]
}
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *VCs = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
if ([[VCs objectAtIndex:[VCs count] - 2] isKindOfClass:[ViewController class]])
{
ViewController *view = [[VCs objectAtIndex:[VCs count] - 2]
view.delegate = self;
[self Print:#"Hello"];
[view addNumber:2];
[view getTotal];
}
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)Print:(NSString *)str{
NSLog(#"%#",str);
}
#end
I have taken the instance of ViewController already present in navigation stack , changed the values and on back I have pop out the SecondViewController.

PDF in UIWebView not working

I have two UIPopoverController. each one opens up a pdf in a UIWebViewDelegate when I click the respective buttons.
However,only one works.
Both buttons, A and B, have this format for the click function (except Button B would say ButtonB not ButtonA)
- (IBAction)openPDF_Pictures_A:(id)sender
{
ButtonAPDFViewController *DWV =[[ButtonAPDFViewController alloc] initWithNibName:nil bundle:nil];
self.masterPopOverController = [[UIPopoverController alloc] initWithContentViewController:DWV];
[self.masterPopOverController presentPopoverFromRect:[sender frame] inView:[sender superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[self.masterPopOverController setPopoverContentSize:CGSizeMake(600, 800) animated:NO];
}
And in the UIViewController for each UIWebView, is this:
#import "ButtonAViewController.h"
#interface ButtonAViewController ()
#end
#implementation ButtonAViewController
#synthesize buttonAPDFView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"partA" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[buttonAPDFView loadRequest:request];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Both buttons A and B are on the same view. I did make one of the IBActions use masterPopOverController2 and one of the masterPopOverController to see if that would resolve anything but it doesn't. The one that doesn't work opens up a popover but it doesn't show the PDF in the popover. Any suggestions?
just realized I didn't add the UIWebView inside the UIView in the xib...

Only black screen in my UIWebView

I have two View Controllers. In the first one i have a button and when you click it you need to display a Web Page in a second View Controller. When I click the button, I see only a black screen.
Here is TabulkaViewController.m code:
#import "TabulkaViewController.h"
#import "WebViewController.h"
#interface TabulkaViewController ()
#end
#implementation TabulkaViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_hbutton.layer.borderWidth = 1;
_hbutton.layer.borderColor = [[UIColor grayColor] CGColor];
}
- (IBAction)hbutton:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://apple.com"];
WebViewController *webViewController = [[WebViewController alloc] initWithURL:url andTitle:#"Apple"];
[self presentViewController:webViewController animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
Here is code of WebViewController.h:
#import <UIKit/UIKit.h>
#interface WebViewController : UIViewController <UIWebViewDelegate>
{
NSURL *theURL;
NSString *theTitle;
IBOutlet UIWebView *webView;
IBOutlet UINavigationItem *webTitle;
}
- (id)initWithURL:(NSURL *)url;
- (id)initWithURL:(NSURL *)url andTitle:(NSString *)string;
- (IBAction) done:(id)sender;
#end
And here is code of WebViewController.m:
#import "WebViewController.h"
#interface WebViewController ()
#end
#implementation WebViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
webTitle.title = theTitle;
NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
[webView loadRequest:requestObject];}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (id)initWithURL:(NSURL *)url andTitle:(NSString *)string {
if( self = [super init] ) {
theURL = url;
theTitle = string;
}
return self;
}
-(id)initWithURL:(NSURL *)url {
return [self initWithURL:url andTitle:nil];
}
- (IBAction) done:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
webView.delegate = nil;
[webView stopLoading];
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
You need to instantiate the webView at some point. I suggest you change your viewDidLoad to this:
- (void)viewDidLoad
{
[super viewDidLoad];
webView = [[UIWebView alloc] init];
webTitle.title = theTitle;
NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
[webView loadRequest:requestObject];
}
Since your webView is nullat the start of the method, it needs to be created before you can load any Requests. Just declaring it in the header file is not enough, thats why you need webView = [[UIWebView alloc] init];.
EDIT:
Of course, you could also put that line into your ìnitWithURL:andTitle: method, where the other instantiations take place.

Sending information from a view controller to another

The question is, I have 9 buttons on viewcontroller (ViewController), and i have an outlet collection for storing all these buttons outlet.
Then i have an action method for handling click event of these buttons.
What I want is send the button image background (as an UIImage * img)of the CLICKED button, segue to another viewcontroller (vc2),
here are my code :
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardBtns;
- (IBAction)cardAction:(id)sender;
#end
#import "ViewController.h"
#import "ResultViewController.h"
#interface ViewController ()
#property(strong,nonatomic)UIImage *image;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)cardAction:(id)sender {
for(UIButton *cardButton in self.cardBtns){
self.image = [cardButton currentBackgroundImage];
}
[self performSegueWithIdentifier:#"cardSegue" sender:self.image];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//UIImage * img = [UIImage imageNamed:#"f1"];
UIImage * img = (UIImage *)sender;
ResultViewController *viewcontroller = [segue destinationViewController];
viewcontroller.img = img;
}
#end
in vc2, i have another button here, and i wish to paint this button background img with the img just sent from vc1 segue.
#interface ResultViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIButton *cardBtn;
#property(strong,nonatomic)UIImage *img;
- (IBAction)cardDismiss:(id)sender;
#end
#import "ResultViewController.h"
#interface ResultViewController ()
#end
#implementation ResultViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.cardBtn setImage:self.img forState:UIControlStateNormal];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)cardDismiss:(id)sender {
[self dismissViewControllerAnimated:NO completion:nil];
}
#end
assume my buttons is 1,2,3,4,5,6,7,8,9
for now, whatever buttons i clicked in vc1, it s always displaying as '6' in vc2...
any suggestions? thanks
The problem is this:
- (IBAction)cardAction:(id)sender {
for(UIButton *cardButton in self.cardBtns){
self.image = [cardButton currentBackgroundImage];
}
[self performSegueWithIdentifier:#"cardSegue" sender:self.image];
}
This will set self.image to the last item in self.cardBtns. You want to set self.image to sender.currentBackgroundImage (and change id in the argument type to UIButton *), and just eliminate the loop.

Resources