I have one class UIViewController and Inside I have one button , I want to call one method from another class
would you please help me to implement the action for my button
I'm new to objective-c
my button Action in UIViewController:
- (IBAction)ActionButton:(id)sender {
NSLog(#"A1");
}
my method in WebViewController:
-(void)loadWebView{
NSURL *url = [NSURL fileURLWithPath:[ [ NSBundle mainBundle ] pathForResource:
#"TestName/TestName1/Test1Name1" ofType:#"html" ]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
my question is how to call this method to my button since it's in another view?
I have one method in my WebViewController and I want to call it from one button that I have in UIViewController
You could try this:
Say you have a class called ViewA which implements the method.
If you want the button action to invoke that method, you need to have an instance of another class.
-(void)loadWebView{
NSURL *url = [NSURL fileURLWithPath:[ [ NSBundle mainBundle ] pathForResource:
#"TestName/TestName1/Test1Name1" ofType:#"html" ]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
For you button VIEW:
#import "ViewA.h"
ViewA *instanceOfViewA;
// assign an instance to instanceOfViewA
Your Button method:
- (IBAction)ActionButton:(id)sender {
NSLog(#"A1");
[yourButton addTarget:instanceOfViewA
action:#selector(loadWebView)
forControlEvents:UIControlEventTouchUpInside];
}
You can use notification or delegate or have the reference of WebViewController in your UIViewController ..
You may store a reference to WebViewController in your main view controller and send a message to that reference, check my answer.
code:
in MyViewController.h:
#class WebViewController;
...
#interface MyViewController
{
...
}
#property (nonatomic, retain) IBOutlet WebViewController* webViewController;
...
in MyViewController.m:
#import "WebViewController.h"
...
#implementation MyViewConroller
#synthesize webViewController;
...
- (void) dealloc {
...
self.webViewController = nil;
...
[super dealloc];
}
Assign your WebViewController (like you did with buttons and stuff) in IB and then in code:
- (IBAction)ActionButton:(id)sender {
NSLog(#"A1");
[self.webViewController loadWebView];
}
Related
I really need help please. Initially, by tapping on a tab bar item, I will load a webview on Gym VC user will then scroll down the web view
What I am trying to accomplish is when user tap on the same tab bar item again, the webView will reload again. I managed to call the method handleRefresh from MyTabBarController But the method doesn't do anything at all. Any guidance is much appreciated.
Currently I have the following code
MyTabBarController.m
#import "GymNavigationController.h"
#import "Gym.h"
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if([viewController isKindOfClass:[GymNavigationController class]]){
Gym *myGym = [[Gym alloc] init];
[myGym handleRefresh:nil];
}
}
Gym.m
#import "Gym.h"
#import "AppDelegate.h"
#import "GymDet.h"
#import <QuartzCore/QuartzCore.h>
#interface Gym () {
AppDelegate *appDelegate;
NSString *sURL, *sRefresh;
}
#end
#implementation Gym
#synthesize webView;
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
sURL = appDelegate.gURL;
sURL = [sURL stringByAppendingString:#"/apps/gym.asp?"];
NSLog(#" The sURL to be load for the current page : %# ", sURL);
sRefresh = sURL;
[defaults setObject:sRefresh forKey:#"txtRefresh"];
[defaults synchronize];
NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[webView loadRequest:urlRequest];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[webView.scrollView addSubview:refreshControl];
}
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//[self handleRefresh:nil];
[[self navigationController] setNavigationBarHidden:YES animated:YES];
}
-(void)viewDidDisappear:(BOOL)animated{
[[self navigationController] setNavigationBarHidden:NO animated:YES];
}
- (void)handleRefresh:(UIRefreshControl *)refresh {
//--- Call the stored txtMemCode, something like session ---
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
sRefresh = [defaults objectForKey:#"txtRefresh"];
NSURL *url = [NSURL URLWithString:sRefresh];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[refresh endRefreshing];
}
First i would like to suggest why you're initiating a new Gym object every time tabbar is tapped you should take a reference of the Gym object and initialise it if its nil then process ahead with the reference, but it maybe your requirement and if it is i would like to suggest you to create a BOOL variable lets say isUpdateRequire and when you're instantiating your viewcontroller assign isUpdateRequire = true. Then in the end of view did load or view will appear (according to your need) check
if isUpdateRequire {
[self handleRefresh:nil]
}
Alternatively you can create protocols in your webviewcontroller and assign its delegate to your tabbarcontroller and fire the delegate method when ever required.
and if you don't want the method to call when you come back to vc simply put this condition in viewWillAppear
if self.isMovingToParentViewController {
[self handleRefresh:nil]
}
I want to reload a WebView whenever a certain menu item is clicked. Here's my code that's not working. (Assume normal declarations in .h files.)
// AppDelegate.m
#import "AppDelegate.h"
// #import "ViewController.h" (in AppDelegate.h)
#interface AppDelegate ()
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
// Menu Item triggers this:
- (IBAction)reloadWebView:(id)sender {
NSLog(#"AppDelegate reloadWebView");
ViewController * vc = [[ViewController alloc] init];
[vc reloadWebView:sender]; // tried this,
[vc.webview reload:sender]; // and this,
[[vc.webview mainFrame] reload]; // and this,
[[vc.webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://example.com/"]]]; // and this.
}
#end
// ViewController.m
#import "ViewController.h"
// #property (assign) IBOutlet WebView *webview; (in ViewController.h)
#implementation ViewController
#synthesize webview;
- (void)viewDidLoad {
[super viewDidLoad];
[[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://example.com/"]]]; // this works
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
// Update the view, if already loaded.
}
// Called in AppDelegate:
- (void)reloadWebView:(id)sender {
NSLog(#"ViewController reloadWebView");
// The problem here seems to be: webview == nil
[webview reload:sender]; // also tried this,
[[webview mainFrame] reload]; // and this,
[[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://example.com/"]]]; // and this.
}
#end
This is all my code. I've just started working on this project.
I've tried using a button in the ViewController. That works, but I need this to be a menu item.
Thanks in advance!
instead of creating a new view controller:
ViewController * vc = [[ViewController alloc] init];
try:
ViewController * vc = (ViewController *)[[[NSApplication sharedApplication] mainWindow] contentViewController];
to call the current view controller
I am trying to load a UIWebview and am trying to implement the SVProgress HUD when the webview is loading and then dismissing it when its loaded.I have almost completed it however when it loads the progress hud shows for a second and then dosent show and then shows for a second and so fourth, like its blinking I'm kinda stuck, check out my code.
In my .h:
#import <UIKit/UIKit.h>
#import "SVProgressHUD.h"
#interface ViewController2 : UIViewController <UIWebViewDelegate> {
//Instantiated the timer varibale inside the interface.
NSTimer *timer1;
IBOutlet UIWebView *webView;
}
//Instantiate the webview ans NSString link.
#property(nonatomic,retain) UIWebView *webView;
#property (strong, nonatomic) NSString *link;
and in my .m
#import "ViewController2.h"
#interface ViewController2 ()
#end
#implementation ViewController2
#synthesize webView;
- (void)viewDidLoad
{
[super viewDidLoad];
//Getting the link from the NSString called "link" and putting in the request.
NSString *link = [NSString stringWithString:self.link];
NSURL *URL = [NSURL URLWithString:link];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[webView loadRequest:request];
//Set the time for the loagind modal view.
timer1 = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:#selector(loading) userInfo:nil repeats:YES];
//Webview Properties
//Webview's name is web, which was instantianted in the propeties section.
webView.ScalesPageToFit = true;
webView.backgroundColor = [UIColor pomegranateColor];
}
//When the UIWebview is loading present "Fetching Homework" alert, thanks to SVProgressHUD
-(void)loading {
if(!webView.loading)
[SVProgressHUD dismiss];
else
[SVProgressHUD showWithStatus:#"Loading Homework"];
}
I think that it could just be the timer, but I'm not sure, anyhelp would be greatly appreciated.
Thanks in advance.
You should use the web view delegate to know when the page ends loading instead of using a timer to check it each 0.5 seconds. Try it this way:
Begin the SVProgressHUD when inits the page loading
[SVProgressHUD showWithStatus:#"Loading Homework"];
Then remove it when the page finish loading.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[SVProgressHUD dismiss];
}
Don't forget that you also have to set your webview delegate as this:
webView.delegate = self;
I'm trying to launch a URL with the UIWebView upon launch of my Mac application.
I started out with this,
ViewController.h
#import <Cocoa/Cocoa.h>
#interface ViewController : UIViewController {
IBOutlet UIWebView *webView;
}
#property (nonatomic, retain) UIWebView *webView;
#end
ViewController.m
#import "ViewController.h"
- (void)viewDidLoad {
NSString *urlAddress = #"http://google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
With that, I get two errors that I can't figure out.
(ViewController.h) #interface ViewController : UIViewController {
Error - Cannot find interface decleration 'UIViewController', superclass of 'ViewController'
(ViewController.m) - (void)viewDidLoad {
Error - Missing context for method declaration
I'm not really familiar with is and can't find what I'm doing wrong.
For OSX app, you don't have UIViewController, instead, you have NSWindowController and NSViewController, check Mac Developer Library.
You are using IOS functions instead of OSX.
Try this
In youy .h file
#import <WebKit/WebKit.h>
WebView *myWebView;
In your .m file
- (void)windowDidLoad
{
NSString *urlAddress = #"http://google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[myWebView mainFrame] loadRequest:requestObj];
}
Don't forget to include "webkit.framework"
Hi everyone i got a problem to play a video in iOS 5. When I'm building the program i get a warning "local declaration of hides instance variables" and when i run the program and tap the button i get a "SIGABRT". And i don't know what to do, all the help i can get is aprriciated!!
Thx in advance
MY .h file
#import <MediaPlayer/MediaPlayer.h>
#interface MainViewController : UIViewController {
MPMoviePlayerController *moviePlayer;
}
-(IBAction)Video:(id)sender;
#end
MY .m file
#import "MainViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController
-(IBAction)Video:(id)sender {
MPMoviePlayerController *moviePlayer;
NSString *path = [[NSBundle mainBundle] pathForResource:#"big-buck-bunny-clip" ofType:#"m4v"];
NSURL *videoURL = [NSURL fileURLWithPath:path];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer setControlStyle:MPMovieControlStyleDefault];
[moviePlayer.view setFrame: self.view.bounds];
[self.view addSubview: moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
local declaration of hides instance variables
remove the first line of your -(IBAction)Video:(id)sender method.
MPMoviePlayerController *moviePlayer; //this line;
because you already have declared such variable in your .h file.
Means you declared the same variable names two times, one in .h file and another in .m file.