I want to make test, but I do not know how to make it (I can use GHUnit).
This is the class I want to test:
Communicator.h
#interface Communicator : NSObject
-(void)loadURLWithString:(NSString*)urlString withWebView:(UIWebView*)webView;
#end
Communicator.m
#import "Communicator.h"
#implementation Communicator
##### I WANT TO EXCLUDE THE WEBVIEW BELOW #####
-(void)loadURLWithString:(NSString*)urlString withWebView:(UIWebView*)webView
{
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSURLRequest *r = [[NSURLRequest alloc] initWithURL:url];
[webView loadRequest:r];
}
#end
And this is ViewController which control Communicator class.
ViewController.h
#import "Communicator.h"
#interface ViewController : UIViewController <UITextFieldDelegate>
{
Communicator *communicator;
}
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#property (weak, nonatomic) IBOutlet UIButton *startButton;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
communicator = [[Communicator alloc] init];
[_startButton addTarget:self action:#selector(startButtonPushed) forControlEvents:UIControlEventTouchDown];
}
- (void)startButtonPushed
{
[communicator loadURLWithString:#"http://www.apple.co.jp" withWebView:_webView];
}
#end
Thank you for your help.
What is it exactly you want to test? That pressing the button causes the webview to load a URL or that the webview actually brings the URL content?
Related
I'm pretty new to classes in objective-c. I'm testing out one I made unhiding an image from the class on the click of a button, everything compiles and runs but it's not working out for me, nothing shows up when I click. Am I doing this all wrong? Tried to include everything I think is relevant in the code below, obviously the images for the other parts of the class are there but I didn't include them below seeing as I'm just testing one image from it.
question.m file -
#import "Question.h"
#implementation Question
#synthesize image;
#end
question.h -
#import <UIKit/UIKit.h>
#import "ViewController.h"
#ifndef Question_h
#define Question_h
#endif /* Question_h */
#interface Question : NSObject
{
UIImageView *image;
}
#property(nonatomic, retain) UIImageView *image;
#end
#class Question;
#interface Controller : NSObject
{
Question *firstQuestion;
Question *secondQuestion;
Question *thirdQuestion;
}
#property(nonatomic, retain) Question *firstQuestion;
#property(nonatomic, retain) Question *secondQuestion;
#property(nonatomic, retain) Question *thirdQuestion;
-(void) Question;
ViewController.m -
#import "ViewController.h"
#import "Question.h"
#synthesize question1, q2, q3, firstaid;
- (void)viewDidLoad {
[super viewDidLoad];
firstTierQuestions = [[NSMutableArray alloc] initWithCapacity:100];
}
- (IBAction)Question:(id)sender {
Question *firstQuestion = [[Question alloc] init];
firstQuestion.image = question1;
firstQuestion.a1 = _answer1;
firstQuestion.a2 = _answer2;
[firstTierQuestions addObject:firstQuestion];
- (IBAction)generator:(id)sender {
for (Question *firstQuestion in firstTierQuestions) {
[firstQuestion.image setHidden:NO];
}
}
viewController.h -
#import "question.h"
#interface ViewController : UIViewController
{
IBOutlet UIImageView *question1;
NSMutableArray *firstTierQuestions;
}
#property (strong, nonatomic) IBOutlet UIButton *generator;
#property (strong, nonatomic) IBOutlet UIImageView *question1;
- (IBAction)generator:(id)sender;
#property (nonatomic, strong) NSObject *Question;
#end
i have first display start_game screen than after when i click button that time display popupview using xib.in xib class i have create delegate method.when i close the popupview that time call delegate method but not calling
here is my delegate class
.h File
#import
#protocol digbuttonalertdelegate;
#interface digbuttonalert : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *bg_image;
#property (weak, nonatomic) IBOutlet UILabel *lbl_title;
#property (nonatomic, weak) id<digbuttonalertdelegate> delegate;
#end
#protocol digbuttonalertdelegate <NSObject>
#optional
-(void)digalertclose;
#end
.m File
#import "digbuttonalert.h"
#import "suggestion_alert.h"
#import "UIViewController+CWPopup.h"
#import "zoom_alert.h"
#interface digbuttonalert ()
{
bool status;
}
#end
#implementation digbuttonalert
- (void)viewDidLoad {
[super viewDidLoad];
status=0;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected)];
singleTap.numberOfTapsRequired = 1;
[self.bg_image setUserInteractionEnabled:YES];
[self.bg_image addGestureRecognizer:singleTap];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)close:(id)sender {
}
-(void)tapDetected{
NSLog(#"single Tap on imageview");
if(status==0)
{
self.lbl_title.text=#"As you walk you will discover the hidden map.This circle will show your progress.";
status=1;
}
else
{
[self dismissViewControllerAnimated:YES completion:nil];
if ([self.delegate respondsToSelector:#selector(digalertclose)]) {
[self.delegate digalertclose];
}
}
}
here this class i want to call method
#import "digbuttonalert.h"
#interface start_games () <MJSecondPopupDelegate,digbuttonalertdelegate>
{
- (void)viewDidLoad {
digbuttonalert *next=[[digbuttonalert alloc]init];
next.delegate=self;
next.modalTransitionStyle=UIWebPaginationModeRightToLeft;
next.modalPresentationStyle=17;
[self presentViewController:next animated:YES completion:nil];
}
- (void)digalertclose
{
[self StartTimer];
[[NSUserDefaults standardUserDefaults]setObject:#"false" forKey:#"alertstatus"];
}
Change .m file with below :
#protocol digbuttonalertdelegate <NSObject>
#optional
-(void)digalertclose;
#end
#interface digbuttonalert : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *bg_image;
#property (weak, nonatomic) IBOutlet UILabel *lbl_title;
#property (nonatomic, weak) id<digbuttonalertdelegate> delegate;
#end
And follow basic tutorial steps : enter link description here
Please implement digalertclose delegate in start_games controller like this
-(void)digalertclose {
}
You make digalertclose optional thats why crash does not occur if you remove optional keyword from digbuttonalertdelegate protocol you can see that there will be a crash because you try to fire a delegate but does not implement it.
In your digbuttonalert.h protocol is defined twice.
There is no #end for the interface.
Should be something like
ClassA.h
#protocol YourProtocol <NSObject>
#optional
-(void)yourProtocolOptionalMethod
#end
#interface ClassA.h
#property (weak, nonatomic) id <YourProtocol> delegate;
#end
ClassA.m
-(void) someClassAMethod {
[self.delegate yourProtocolOptionalMethod];
}
ClassB.h
#import "ClassA.h"
#interface ClassB <YourProtocol>
#end
ClassB.m
-(void) someClassBMethod {
ClassA *classA = [ClassA alloc] init];
classA.delegate = self;
}
After setting the delegate now when you will call the delegate from ClassA then it will trigger the protocol implemented method in ClassB
-(void)yourProtocolOptionalMethod {
NSlog(#"");
}
On your class , you need to declare the delegate like this:
#interface YourViewController () < digbuttonalertdelegate >
Hi everyone I have been building an app and have met some problems.
My app has two viewControllers. One is MenuViewController and another one is MainViewController.
I want to pass a string from MainViewController to a mutableArray in MenuViewController, but have no idea how.
Here are my codes:
<MenuViewController.h>
#import <UIKit/UIKit.h>
#interface MenuViewController : UITableViewController {
NSMutableArray *secondFavourite;
}
#property (nonatomic, strong) NSMutableArray *secondFavourite;
#end
.
<MenuViewController.m>
#import "MenuViewController.h"
#import "MainViewController.h"
#interface MenuViewController ()
#property (strong, nonatomic) NSArray *menu;
#end
#implementation MenuViewController
#synthesize menu;
#synthesize secondFavourite;
- (void)viewDidLoad
{
[super viewDidLoad];
self.secondFavourite = [[NSMutableArray alloc] init];
self.menu = self.secondFavourite;
}
.
<MainViewController.h>
#import <UIKit/UIKit.h>
#import <social/Social.h>
#interface MainViewController : UIViewController {
IBOutlet UIImageView *imagepost;
UILabel *predictionLabel;
}
- (IBAction)sampleSelector:(UIButton *)sender;
- (IBAction)showAllClick:(id)sender;
#property (nonatomic, retain) IBOutlet UILabel *predictionLabel;
#property (strong, nonatomic) NSArray *predictionArray;
#property (strong, nonatomic) UIButton *menuBtn;
#property (strong, nonatomic) NSMutableArray *fav;
#property (strong, nonatomic) IBOutlet UILabel *favLabel;
#property (strong, nonatomic) IBOutlet UITableView* tableView;
#property (nonatomic, strong) NSMutableArray *favourite;
#end
.
<MainViewController.m>
- (void)viewDidLoad
{
[super viewDidLoad];
self.predictionArray = [[NSArray alloc] initWithObjects:#"Hey gurl", nil];
}
//Add to favourite
- (void) addToFav {
self.favourite = [[NSMutableArray alloc] init];
[self.favourite addObject:self.predictionLabel.text];
[self.tableView reloadData];
NSLog(#"%#", self.favourite);
}
//add to favourite button action
- (IBAction)addToFavButton:(id)sender {
[self addToFav];
//pass data from favourite here to secondFacourite in MenuViewController (found on stack overflow)
MenuViewController *menuViewController = [[MenuViewController alloc]initWithNibName:#"MenuViewController" bundle:nil];
menuViewController.secondFavourite = [[NSMutableArray alloc]initWithArray:self.favourite];
[self.navigationController pushViewController:menuViewController animated:YES];
}
I used NSLog to check that the menuViewController.secondFavourite in MainViewController successfully added the string into the array, isn't the array the same array in MenuViewController? Why doesn't the menu.tableView update and show the new string added? I am very confused and I hope someone will help me out.
Thanks for reading this.
This has to do with the fact that your menu viewDidLoad is overwriting the value in these two lines:
self.secondFavourite = [[NSMutableArray alloc] init];
self.menu = self.secondFavourite;
That first line is setting your secondFavourite property to an empty NSMutableArray instance. And since viewDidLoad will be called only after the view has been loaded into memory (in this case, when you try to push the view controller onto the stack), your initial values in the secondFavourite property will be lost.
Instead, you should move that initialization code into the an init method.
I am making a music streaming app but i can only seem to add the controls to the default view controller this is my code for ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UIWebView *WebView;
}
-(IBAction)Play:(id)sender;
#end
and this is my code for viewcontrol.m
-(IBAction)Play:(id)sender{
NSString *stream = #"http://117.53.175.113:15008/listen.pls";
NSURL *url = [NSURL URLWithString:stream];
NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];
[WebView loadRequest:urlrequest];
}
#end
Please help
Drag you IBOutlet from interface builder and to outside class parentheses {}.
Something like:
#interface ViewController : UIViewController {
IBOutlet UIWebView *WebView; // remove this.
}
#property (unsafe_unretained, nonatomic) IBOutlet UIWebView * WebView;
I want to add search theWebView for UIWebView, and i need to put search query from UIWebView to the function of button:
-(IBAction)searchButtonPressed:(id)sender{
[theWebView highlightAllOccurencesOfString:#"%#", searchR];
}
ViewController.h:
#interface ViewController : UIViewController <UIWebViewDelegate, UIScrollViewDelegate> {
IBOutlet UIWebView *theWebView;
IBOutlet UITextField *searchF;
IBOutlet UILabel *searchR1;
NSString *searchR;
}
#property (retain, nonatomic) IBOutlet UIWebView *theWebView;
#property (retain, nonatomic) IBOutlet UITextField *searchF;
-(IBAction)searchButtonPressed:(id)sender;
-(IBAction)clearHighlights:(id)sender;
ViewController.m:
#import "ViewController.h"
#import "SearchWebView.h"
#implementation ViewController
#synthesize theWebView;
#synthesize searchF;
-(IBAction)searchButtonPressed:(id)sender{
[searchF resignFirstResponder];
searchR = searchF.text;
searchR1.text = [[NSString alloc] initWithFormat:#"%#", searchR];
/*[theWebView highlightAllOccurencesOfString:#"%#", searchR];
[theWebView highlightAllOccurencesOfString:#"7019"];*/
}
-(IBAction)clearHighlights:(id)sender{
[theWebView removeAllHighlights];
}
This is my try, but it don't work!
Help please