I have a problem with webview.
I made a simple webbrowser for Osx, within I have to hide nvigation bar , menu amd right click and the user can go only in one specific url..
all this is ok but I need that allow _blank target.. i mean .. I have some link woth target _blank, so to open in a new window, but it does no work and i don't know hot to allow this.
this is my code for DataOAppDelegate.h:
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface DataOAppDelegate : NSObject <NSApplicationDelegate,NSWindowDelegate>
{WebView *WebView;
//other instance variable
}
#property (assign) IBOutlet NSWindow *window;
#property (retain, nonatomic) IBOutlet WebView *myWebView;
#end
and code for DataOAppDelegate.m
#import "DataOAppDelegate.h"
//#import <WebKit/WebKit.h>
#implementation DataOAppDelegate
#synthesize window;
#synthesize myWebView;
//your function etc
-(void)awakeFromNib{
NSString *urlText = #"http://website.com";
[[myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
[myWebView setDrawsBackground:NO];
[window setDelegate:self];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[myWebView setUIDelegate:self];
NSString *urlText = #"http://website.com";
[[myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
}
- (WebView *)myWebView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request
{
NSLog(#"sss%#",sender);
NSUInteger windowStyleMask = NSClosableWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask |
NSTitledWindowMask;
NSWindow* webWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600) styleMask:windowStyleMask backing:NSBackingStoreBuffered defer:NO];
WebView* newWebView = [[WebView alloc] initWithFrame:[webWindow contentRectForFrameRect:webWindow.frame]];
[newWebView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
[webWindow setContentView:newWebView];
[webWindow center];
[webWindow makeKeyAndOrderFront:self];
[[newWebView mainFrame] loadRequest:request];
return newWebView;
}
- (void)launchSoftWithBundleID:(NSString *)softPath
{
NSBundle *softBundle = [NSBundle bundleWithPath:softPath];
NSString *bundleID = [softBundle bundleIdentifier];
//
NSTask *softTask = [[NSTask alloc] init];
[softTask setLaunchPath:softPath];
[softTask launch];
//
NSArray *array = [NSRunningApplication runningApplicationsWithBundleIdentifier:bundleID];
if ([array count] > 0)
{
NSRunningApplication *runningApp = [array objectAtIndex:0];
[runningApp activateWithOptions:NSApplicationActivateIgnoringOtherApps];
}
}
WebViews have delegate methods to do so: decidePolicyForNavigationAction and decidePolicyForNewWindowAction (documentation).
- (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id)listener {
if ([sender isEqual:self.YourVebView]) {
[listener use];
}
else {
[[NSWorkspace sharedWorkspace] openURL:[actionInformation objectForKey:WebActionOriginalURLKey]];
[listener ignore];
}
}
- (void)webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request newFrameName:(NSString *)frameName decisionListener:(id<WebPolicyDecisionListener>)listener {
[[NSWorkspace sharedWorkspace] openURL:[actionInformation objectForKey:WebActionOriginalURLKey]];
[listener ignore];
}
Note: don't forget to set the policy delegate for your WebView.
Related
My app is Objective-C, and I'm migrating little by little, so I need help with Objective-C, not Swift.
I'm migrating UIWebView to WKWebView and having problems to make didFinish to work.
This the class where I use the WebView, TMAnswerView:
#import "TMAnswerView.h"
#import "TMConsts.h"
#import "TMPersistanceManager.h"
#import <WebKit/WebKit.h>
#interface TMAnswerView () <WKNavigationDelegate>
//UIWebViewDelegate
#end
#implementation TMAnswerView.m
-(void)customInit{
}
-(void)setAnswer:(TMAnswerModel *)answer{
_answer = answer;
float font = 17;
NSNumber *type = [TMPersistanceManager fetchObjectForKey:PERSettingsFontSize];
if([type isEqual:SettingsFontSizeType1]){
font = font * 0.75;
}else if([type isEqual:SettingsFontSizeType3]){
font = font * 1.25;
}else if([type isEqual:SettingsFontSizeType4]){
font = font * 1.5;
}else if([type isEqual:SettingsFontSizeType5]){
font = font * 2;
}
NSString *htmlBody = [TMUtils getHTMLStringForMath:[answer.answer stringByReplacingOccurrencesOfString:#"$$" withString:#"$"] andFontSize:(int)font];
[_answerWebView loadHTMLString:htmlBody baseURL:[NSURL fileURLWithPath: [NSString stringWithFormat:#"%#/", [[NSBundle mainBundle] bundlePath]]]];
_answerWebView.scrollView.contentInset = UIEdgeInsetsMake(0,-8,0,-8);
}
#pragma mark - click listeners
- (IBAction)onCheckButton:(id)sender {
if(_viewControllerType != TMMainTestViewConstrollerTypeDoTest){
return;
}
_checkButton.selected = !_checkButton.selected;
if(_delegate){
[_delegate onCheckChanged:_answer];
}
}
- (void)webView:(WKWebView *)webView
didFinishNavigation:(WKNavigation *)navigation{
[self setWebViewHeight];
}
//-(void)webViewDidFinishLoad:(WKWebView *)webView{
// [self setWebViewHeight];
//}
-(void) setWebViewHeight{
CGSize fittingSize = [_answerWebView sizeThatFits:CGSizeZero];
_heightOfWebView.constant = fittingSize.height;
}
#end
Here I've replaced UIWebView delegate by WKNavigationDelegate. I have to mention that is working fine with the old webViewDidFinishLoad of the UIWebView.
TMAnswerView.h:
#import "TMCustomView.h"
#import "TMAnswerModel.h"
#import "TMMainTestViewController.h"
#import <WebKit/WebKit.h>
#protocol TMAnswerViewProtocol <NSObject>
-(void) onCheckChanged:(TMAnswerModel*) answer;
#end
#interface TMAnswerView : TMCustomView
#property (nonatomic, strong) TMAnswerModel *answer;
#property (weak, nonatomic) IBOutlet UIButton *checkButton;
#property (weak, nonatomic) IBOutlet WKWebView *answerWebView;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightOfWebView;
#property (weak, nonatomic) id<TMAnswerViewProtocol> delegate;
#property (nonatomic) TMMainTestViewConstrollerType viewControllerType;
-(void) setWebViewHeight;
#end
And finally in the storyboard I've added a WebKitView element in replacement of the old UIWebView.
I also tried with didFinishNavigation to no avail.
I checked this next page as reference:
WKWebView
Is there anything wrong in my code? How can I make the didFinish event trigger?
Edit 1
I also tried:
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
[self setWebViewHeight];
}
to no avail.
Edit 2
This is the class where the WKWebView is load, TMQuestionView:
#import "TMQuestionView.h"
#import "TMColors.h"
#import "TMDBManager.h"
#import "TMConsts.h"
#import "TMAnswerModel.h"
#import "TMAnswerView.h"
#import "TMViewUtils.h"
#import "TMPersistanceManager.h"
#import "TMImagePreviewView.h"
#import <WebKit/WebKit.h>
#interface TMQuestionView () <TMAnswerViewProtocol, WKNavigationDelegate>
#property (weak, nonatomic) IBOutlet WKWebView *webView;
#property (weak, nonatomic) IBOutlet UIView *answersView;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *webviewHeight;
#property (weak, nonatomic) IBOutlet UIView *viewForLoading;
#property (weak, nonatomic) IBOutlet UIView *loadingView;
#property (weak, nonatomic) IBOutlet WKWebView *webviewExplanations;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *webviewExplanationsHeight;
#property (nonatomic) BOOL isExplanationsVisible;
#property (nonatomic, strong) NSMutableArray *images;
#property (nonatomic, strong) NSString *tempic;
#end
#implementation TMQuestionView
-(void)customInit{
[[CSLoadingManager sharedManager] addLoadingViewToView:_viewForLoading withColor:TMBaseColor(1)];
_images = [NSMutableArray new];
}
-(void)setQuestion:(TMQuestionModel *)question{
_question = question;
float font = 17;
NSNumber *type = [TMPersistanceManager fetchObjectForKey:PERSettingsFontSize];
if([type isEqual:SettingsFontSizeType1]){
font = font * 0.75;
}else if([type isEqual:SettingsFontSizeType3]){
font = font * 1.25;
}else if([type isEqual:SettingsFontSizeType4]){
font = font * 1.5;
}else if([type isEqual:SettingsFontSizeType5]){
font = font * 2;
}
[_images addObjectsFromArray:[TMUtils getImagesFromQuestion:question.question]];
[_images addObjectsFromArray:[TMUtils getImagesFromQuestion:question.instructions]];
NSString *htmlString = question.question;
if(question.instructions.length > 0 && ![question.instructions isEqualToString:#"(null)"]){
htmlString = [NSString stringWithFormat:#"%#<br/>%#", question.instructions, question.question];
}
NSString *htmlBody = [TMUtils getHTMLStringForMath:htmlString andFontSize:(int)font];
htmlBody = [htmlBody stringByReplacingOccurrencesOfString:#"<center>" withString:#"<p style='text-align:center;'>"];
htmlBody = [htmlBody stringByReplacingOccurrencesOfString:#"</center>" withString:#"</p>"];
_tempic = htmlBody;
[_webView loadHTMLString:htmlBody baseURL:[NSURL fileURLWithPath: [NSString stringWithFormat:#"%#/", [[NSBundle mainBundle] bundlePath]]]];
_webView.scrollView.contentInset = UIEdgeInsetsMake(0,-8,0,-8);
_answersView.hidden = YES;
[[TMDBManager sharedManager] getAnswersForQuestion:_question completition:^(NSDictionary *dict) {
NSArray *temp = [dict objectForKey:DBReturnAnswers];
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"answerNumber" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
self->_answers = [temp sortedArrayUsingDescriptors:sortDescriptors];
[self setViewAnswers];
}];
}
-(void) setExplanations{
_isExplanationsVisible = YES;
float font = 17;
NSNumber *type = [TMPersistanceManager fetchObjectForKey:PERSettingsFontSize];
if([type isEqual:SettingsFontSizeType1]){
font = font * 0.75;
}else if([type isEqual:SettingsFontSizeType3]){
font = font * 1.25;
}else if([type isEqual:SettingsFontSizeType4]){
font = font * 1.5;
}else if([type isEqual:SettingsFontSizeType5]){
font = font * 2;
}
NSString *htmlString = _question.explanation;
NSString *htmlBody = [TMUtils getHTMLStringForMath:htmlString andFontSize:(int)font];
[_webviewExplanations loadHTMLString:htmlBody baseURL:[NSURL fileURLWithPath: [NSString stringWithFormat:#"%#/", [[NSBundle mainBundle] bundlePath]]]];
_webviewExplanations.scrollView.contentInset = UIEdgeInsetsMake(0,-8,0,-8);
}
-(void)setRecordAnswer:(TMRecordAnswerModel *)recordAnswer{
_recordAnswer = recordAnswer;
}
-(void) setViewAnswers{
int i = 0;
float font = 17;
NSNumber *type = [TMPersistanceManager fetchObjectForKey:PERSettingsFontSize];
if([type isEqual:SettingsFontSizeType1]){
font = font * 0.75;
}else if([type isEqual:SettingsFontSizeType3]){
font = font * 1.25;
}else if([type isEqual:SettingsFontSizeType4]){
font = font * 1.5;
}else if([type isEqual:SettingsFontSizeType5]){
font = font * 2;
}
for(TMAnswerModel *item in _answers){
TMAnswerView *view = [[TMAnswerView alloc] init];
view.translatesAutoresizingMaskIntoConstraints = NO;
[_answersView addSubview:view];
[[view.leadingAnchor constraintEqualToAnchor:_answersView.leadingAnchor constant:0] setActive:YES];
[[view.rightAnchor constraintEqualToAnchor:_answersView.rightAnchor constant:0] setActive:YES];
if(i == 0){
[[view.topAnchor constraintEqualToAnchor:_answersView.topAnchor constant:0] setActive:YES];
}else{
UIView *lastView = [[_answersView subviews] objectAtIndex:i-1];
[[view.topAnchor constraintEqualToAnchor:lastView.bottomAnchor constant:0] setActive:YES];
}
view.answer = item;
view.delegate = self;
view.viewControllerType = _viewControllerType;
if(_recordAnswer){
if(item.isCorrect == 1){
if([_recordAnswer.selectedAnswerId isEqualToString:item.answerId]){
[view.checkButton setImage:[UIImage imageNamed:#"checkbox_checked"] forState:UIControlStateNormal];
}else{
[view.checkButton setImage:[UIImage imageNamed:#"checkbox_checked_gray"] forState:UIControlStateNormal];
}
}else{
if([_recordAnswer.selectedAnswerId isEqualToString:item.answerId]){
[view.checkButton setImage:[UIImage imageNamed:#"checkbox_error"] forState:UIControlStateNormal];
}
}
}
i++;
if(i == [_answers count]){
[[view.bottomAnchor constraintEqualToAnchor:_answersView.bottomAnchor constant:0] setActive:YES];
}
}
}
-(void)onCheckChanged:(TMAnswerModel *)answer{
for (TMAnswerView *item in [_answersView subviews]){
if(![item.answer isEqual:answer]){
if(item.checkButton.selected){
item.checkButton.selected = NO;
}
}
}
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
[self setHeightOfWebView];
}
//-(void)webViewDidFinishLoad:(WKWebView *)webView{
// [self setHeightOfWebView];
//}
-(TMAnswerModel*) getSelectedAnswer{
for (TMAnswerView *item in [_answersView subviews]){
if(item.checkButton.selected){
return item.answer;
}
}
return nil;
}
-(void) setHeightOfWebView{
_viewForLoading.hidden = YES;
_loadingView.hidden = YES;
CGSize fittingSize = [_webView sizeThatFits:CGSizeZero];
_webviewHeight.constant = fittingSize.height;
_answersView.hidden = NO;
for(UIView *item in [_answersView subviews]){
if([item isKindOfClass:[TMAnswerView class]]){
[((TMAnswerView*) item) setWebViewHeight];
}
}
if(_isExplanationsVisible){
CGSize fittingSizeExplanations = [_webviewExplanations sizeThatFits:CGSizeZero];
_webviewExplanationsHeight.constant = fittingSizeExplanations.height;
}
}
- (IBAction)onButtonAboveWebViewClicked:(id)sender {
if([_images count] > 0){
TMImagePreviewView *view = [[TMImagePreviewView alloc] initWithFrame:CGRectMake(0, 0, kAppWidth, kAppHeight)];
[view setImages:_images];
[[[self superview] superview] addSubview:view];
[view fadeIn];
}
}
- (IBAction)onButtonTemp:(id)sender forEvent:(UIEvent *)event {
NSSet *touches = [event touchesForView:sender];
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:[sender superview]];
NSLog(#"%#", NSStringFromCGPoint(touchPoint));
long htmlLength = _tempic.length;
long heightOfWebView = _webView.frame.size.height;
double percentTouch = (double)touchPoint.y / (double)heightOfWebView;
int index = 0;
for(NSString *imageStr in _images){
NSString *match = [[imageStr componentsSeparatedByString:#"/"] objectAtIndex:1];
NSRange rangeOfImage = [_tempic rangeOfString:match];
double percentText = (double) rangeOfImage.location / (double)htmlLength;
if(percentText > percentTouch){
break;
}
index++;
}
NSLog(#"STOP");
}
#end
May be you can add this in TMAnswerView.setAnswer :
-(void)setAnswer:(TMAnswerModel *)answer {
...
// set self as navigationDelegate for the webView
_answerWebView.navigationDelegate = self;
[_answerWebView loadHTMLString:htmlBody...
...
I am developing an app in Xcode (objective-c). My app has a TableView with a list of restaurants and when you press one row, another view is opened with the restaurant information. The method I am using is that I am sending the title in the row to the new view and depending on the title I load the information of the restaurant. I want to do exactly the same using a map pin button. My problem is that I am trying to send the TitleLabel to other ViewController but this is not working and I don't know why.
This is my map pin: RestMapPin.h:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface RestMapPin : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#end
This is my RestMappin.m:
#import "RestMapPin.h"
#implementation RestMapPin
#synthesize coordinate, title, subtitle;
#end
This is my MapViewController.h:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface MapViewController : UIViewController {
MKMapView *mapView;
}
#property (weak, nonatomic) IBOutlet UIBarButtonItem *barButton;
#property (nonatomic, retain) IBOutlet MKMapView *mapView;
-(IBAction)setMap:(id)sender;
#end
This is my MapViewController.m:
#import "MapViewController.h"
#import "SWRevealViewController.h"
#import "RestMapPin.h"
#import "RestViewController.h"
#import "MainTableViewController.h"
#interface MapViewController ()
#end
#implementation MapViewController
#synthesize mapView;
- (void)viewDidLoad {
[super viewDidLoad];
_barButton.target = self.revealViewController;
_barButton.action = #selector(revealToggle:);
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
mapView.delegate = self;
/*Babelia*/
MKCoordinateRegion region_Babelia = { {0.0, 0.0}, {0.0, 0.0}};
region_Babelia.center.latitude = 40.4234778;
region_Babelia.center.longitude = -3.686283000000003;
region_Babelia.span.longitudeDelta = 0.1f;
region_Babelia.span.latitudeDelta = 0.1f;
[mapView setRegion:region_Babelia animated:YES];
RestMapPin *ann_Babelia = [[RestMapPin alloc] init];
ann_Babelia.title = #"Babelia";
ann_Babelia.subtitle = #"Barrio de Salamanca";
ann_Babelia.coordinate = region_Babelia.center;
[mapView addAnnotation:ann_Babelia];
/*Bacira*/
MKCoordinateRegion region_Bacira = { {0.0, 0.0}, {0.0, 0.0}};
region_Bacira.center.latitude = 40.43375390000001;
region_Bacira.center.longitude = -3.699036299999989;
region_Bacira.span.longitudeDelta = 0.1f;
region_Bacira.span.latitudeDelta = 0.1f;
[mapView setRegion:region_Bacira animated:YES];
RestMapPin *ann_Bacira = [[RestMapPin alloc] init];
ann_Bacira.title = #"Bacira";
ann_Bacira.subtitle = #"Chamberí";
ann_Bacira.coordinate = region_Bacira.center;
[mapView addAnnotation:ann_Bacira];
/*Indicador de posicion del mapa (para centrarlo)*/
MKCoordinateRegion region_posicion = { {0.0, 0.0}, {0.0, 0.0}};
region_posicion.center.latitude = 40.44934744420573;
region_posicion.center.longitude = -3.695504665374756;
region_posicion.span.longitudeDelta = 0.08f;
region_posicion.span.latitudeDelta = 0.08f;
[mapView setRegion:region_posicion animated:YES];
/*************************************************/
}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
if(annotation != mapView.userLocation) {
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:#"pin2#2x.png"];
}
else {
//[mapView.userLocation setTitle:#"I am here"];
}
UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//if ([[annotation title] isEqualToString:#"Marieta"]) {
//[pinButton addTarget:self action:#selector(annotationView:) forControlEvents:UIControlEventTouchUpInside];
//}
pinView.rightCalloutAccessoryView = pinButton;
return pinView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
RestMapPin *selectedAnnotation = view.annotation;
NSLog(#"Title of selected pin - %#", selectedAnnotation.title);
// Here you get the title of selected annotation
//RestViewController *objYourVC = [[RestViewController alloc]init];
RestViewController *objYourVC = [self.storyboard instantiateViewControllerWithIdentifier:#"MyIdentifier"];
objYourVC.TitleLabel = selectedAnnotation. title;
NSLog(#"objYourVC.TitleLabel - %#", objYourVC.TitleLabel);
//objYourVC.DetailModal.title = selectedAnnotation.title;
//NSLog(#"objYourVC.TitleLabel - %#", objYourVC.TitleLabel);
objYourVC.DescriptionLabel = selectedAnnotation. subtitle;
NSLog(#"objYourVC.DescriptionLabel - %#", objYourVC.DescriptionLabel);
[self.navigationController pushViewController:objYourVC animated:YES];
}
-(IBAction)setMap:(id)sender {
switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
case 0:
mapView.mapType = MKMapTypeStandard;
break;
case 1:
mapView.mapType = MKMapTypeSatellite;
break;
case 2:
mapView.mapType = MKMapTypeHybrid;
break;
default:
break;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Then I am going to show you the view that where I want to get the data of the objYourVC.TitleLabel
This is my RestViewController.h:
#import <UIKit/UIKit.h>
#import "Restaurant.h"
#interface RestViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *TitleLabel;
#property (strong, nonatomic) IBOutlet UILabel *DescriptionLabel;
#property (strong, nonatomic) IBOutlet UIImageView *ImageView;
//#property (strong, nonatomic) IBOutlet NSArray *DetailModal;
#property (weak, nonatomic) IBOutlet UITextView *phoneTextView;
#property (weak, nonatomic) IBOutlet UIButton *webButton;
#property (weak, nonatomic) IBOutlet UITextView *adressTextView;
#property (weak, nonatomic) IBOutlet UIButton *cartaButton;
#property (weak, nonatomic) IBOutlet UITextView *reviewTextView;
#property (weak, nonatomic) IBOutlet UILabel *updateLabel;
#property (nonatomic, strong) Restaurant *DetailModal;
#end
This is my RestViewController.m:
#import "RestViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface RestViewController ()
#end
#implementation RestViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *decDate = #"11/2016";
if ([_DetailModal.title isEqualToString:#"Babelia"]) {
_TitleLabel.text = _DetailModal.title;
_DescriptionLabel.text = _DetailModal.desc;
_ImageView.image = [UIImage imageNamed:_DetailModal.image];
_phoneTextView.text = #"(+34) 918 31 71 79";
_adressTextView.text = #"\n• Callejón de Puigcerdá, 6, Madrid, España";
[_webButton setTitle:#"Web" forState:UIControlStateNormal];
[_webButton addTarget:self action:#selector(openWeb_babelia_Url) forControlEvents:UIControlEventTouchUpInside];
[_cartaButton setTitle:NSLocalizedString (#"Menu", nil) forState:UIControlStateNormal];
[_cartaButton addTarget:self action:#selector(openCarta_babelia) forControlEvents:UIControlEventTouchUpInside];
_reviewTextView.text = NSLocalizedString(#"No reviews available.", nil);
_updateLabel.text = [NSString stringWithFormat:NSLocalizedString(#"Updated: %#", nil), decDate];
}
if ([_DetailModal.title isEqualToString:#"Bacira"]) {
_TitleLabel.text = _DetailModal.title;
_DescriptionLabel.text = _DetailModal.desc;
_ImageView.image = [UIImage imageNamed:_DetailModal.image];
_phoneTextView.text = #"(+34) 91 866 40 30";
_adressTextView.text = #"\n• Calle Del Castillo, 16, Madrid, España";
[_webButton setTitle:#"Web" forState:UIControlStateNormal];
[_webButton addTarget:self action:#selector(openWeb_bacira_Url) forControlEvents:UIControlEventTouchUpInside];
[_cartaButton setTitle:NSLocalizedString (#"Menu", nil) forState:UIControlStateNormal];
[_cartaButton addTarget:self action:#selector(openCarta_bacira) forControlEvents:UIControlEventTouchUpInside];
_reviewTextView.text = NSLocalizedString(#"No reviews available.", nil);
_updateLabel.text = [NSString stringWithFormat:NSLocalizedString(#"Updated: %#", nil), decDate];
}
}
-(void)openWeb_babelia_Url{
UIApplication *application = [UIApplication sharedApplication];
NSURL *URL = [NSURL URLWithString:#"http://babeliarestaurante.com/contactar/"];
[application openURL:URL options:#{} completionHandler:^(BOOL success) {
if (success) {
NSLog(#"Opened url");
}
}];
}
-(void)openWeb_bacira_Url{
UIApplication *application = [UIApplication sharedApplication];
NSURL *URL = [NSURL URLWithString:#"http://www.bacira.es/contacto/4585843222"];
[application openURL:URL options:#{} completionHandler:^(BOOL success) {
if (success) {
NSLog(#"Opened url");
}
}];
}
-(void)openCarta_babelia{
UIApplication *application = [UIApplication sharedApplication];
NSURL *URL = [NSURL URLWithString:#"http://babeliarestaurante.com/nuestra-cocina/las-cartas/"];
[application openURL:URL options:#{} completionHandler:^(BOOL success) {
if (success) {
NSLog(#"Opened url");
}
}];
}
-(void)openCarta_bacira{
UIApplication *application = [UIApplication sharedApplication];
NSURL *URL = [NSURL URLWithString:#"http://www.bacira.es/carta/4591994095"];
[application openURL:URL options:#{} completionHandler:^(BOOL success) {
if (success) {
NSLog(#"Opened url");
}
}];
}
#end
The problem is that the method calloutAccessoryControlTapped: is not working well because the RestViewController.m is not receiving the objYourVC.TitleLabel. What can I do?
When I run the project, the objYourVC.TitleLabel is good in the MapViewController.m (#"Bacira" for example), but when the project goes to RestViewContoller.m the TitleLable is not good, it shows (#"Label").
I know I have a low level of Xcode and I need your help. I am trying to learn more every day. Can anyone help me? Thank you very much.
Edit: This is my Restaurant.h:
#import <Foundation/Foundation.h>
#interface Restaurant : NSObject
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *desc;
#property (nonatomic, copy) NSString *image;
- (instancetype)init:(NSString *)title descripiton:(NSString *)description image:(NSString *)image;
#end
And this is my restaurant.m:
#import "Restaurant.h"
#implementation Restaurant
- (instancetype)init:(NSString *)title descripiton:(NSString *)description image:(NSString *)image {
self = [super init];
if (self != nil) {
self.title = title;
self.desc = description;
self.image = image;
}
return self;
}
#end
You cannot directly deal with IBoutlet , because they have weak reference. o you need to store it an varaible and then in viewDidLoad you need to update the appropriate IBOutlets.
MapViewController.m
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
RestMapPin *selectedAnnotation = view.annotation;
NSLog(#"Title of selected pin - %#", selectedAnnotation.title);
// Here you get the title of selected annotation
//RestViewController *objYourVC = [[RestViewController alloc]init];
RestViewController *objYourVC = [self.storyboard instantiateViewControllerWithIdentifier:#"MyIdentifier"];
objYourVC.DetailModal.pagetitle= selectedAnnotation.title;
objYourVC.DetailModal.pageDescription = selectedAnnotation.desc;
[self.navigationController pushViewController:objYourVC animated:YES];
}
RestViewController.h:
#import <UIKit/UIKit.h>
#import "Restaurant.h"
#interface RestViewController : UIViewController
#property (nonatomic, strong) NSString *pageTitle;
#property (nonatomic, strong) NSString *pageDescription ;
#end
RestViewController.m:
#import "RestViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface RestViewController ()
#end
#implementation RestViewController
- (void)viewDidLoad {
[super viewDidLoad];
_TitleLabel.text = _pageTitle;
_DescriptionLabel.text = _pageDescription;
}
#end
Hey I was wondering is their any possible way I can link two actions to the same button in Xcode? I've already tried but keep getting this error: "terminating with uncaught exception of type NSException". So i'm guessing I am not able to do that? See what i'm trying to do is
make a button play a sound but that same button is also linked to starting a new round in the game. How would I go about doing this? I've currently got this going in my .m file.
#import "BullsEyeViewController.h"
#interface BullsEyeViewController ()
#end
#implementation BullsEyeViewController
{
int _currentValue;
int _targetValue;
int _score;
int _round;
}
- (IBAction)playSound:(id)sender {
SystemSoundID soundID;
NSString *buttonName=[sender currentTitle];
NSString *soundFile=[[NSBundle mainBundle]
pathForResource:buttonName ofType:#"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)
[NSURL fileURLWithPath:soundFile], &
soundID);
AudioServicesPlaySystemSound(soundID);
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self startNewGame];
[self updateLabels];
UIImage *thumbImageNormal = [UIImage
imageNamed:#"SliderThumb-Normal"];
[self.slider setThumbImage:thumbImageNormal
forState:UIControlStateNormal];
UIImage *thumbImageHighlighted = [UIImage
imageNamed:#"SliderThumb-Highlighted"];
[self.slider setThumbImage:thumbImageHighlighted
forState:UIControlStateHighlighted];
UIImage *trackLeftImage =
[[UIImage imageNamed:#"SliderTrackLeft"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 14, 0, 14)];
[self.slider setMinimumTrackImage:trackLeftImage
forState:UIControlStateNormal];
UIImage *trackRightImage =
[[UIImage imageNamed:#"SliderTrackRight"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 14, 0, 14)];
[self.slider setMaximumTrackImage:trackRightImage
forState:UIControlStateNormal];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)startNewRound
{
_round += 1;
_targetValue = 1 + arc4random_uniform(100);
_currentValue = 50;
self.slider.value = _currentValue;
}
- (void)startNewGame
{
_score = 0;
_round = 0;
[self startNewRound];
}
- (void)updateLabels
{
self.targetLabel.text = [NSString stringWithFormat:#"%d",
_targetValue];
self.scoreLabel.text = [NSString stringWithFormat:#"%d",
_score];
self.roundLabel.text = [NSString stringWithFormat:#"%d",
_round];
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)showAlert
{
int difference = abs(_targetValue - _currentValue);
int points = 100 - difference;
NSString *title;
if (difference == 0) {
title = #"Perfect!";
points += 100;
} else if (difference < 5) {
title = #"You almost had it!";
if (difference == 1) {
points += 50;
}
} else if (difference < 10 ) {
title = #"Pretty good!";
} else {
title = #"Not even close...";
}
_score+=points;
NSString *message = [NSString stringWithFormat:#"You scored %d points", points];
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle: title
message:message
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alertView show];
}
-(IBAction)sliderMoved:(UISlider *)slider
{
_currentValue = lroundf(slider.value);
}
- (void)alertView:(UIAlertView *)alertView
didDismissWithButtonIndex:(NSInteger)buttonIndex
{
[self startNewRound];
[self updateLabels];
}
-(IBAction)startOver
{
CATransition *transition = [CATransition animation];
transition.type = kCATransitionFade;
transition.duration = 1;
transition.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut];
[self startNewGame];
[self updateLabels];
[self.view.layer addAnimation:transition forKey:nil];
}
#end
And here's my .h file.
//
// BullsEyeViewController.h
// BullsEye
//
// Created by Sebastian Shelley on 28/04/2014.
// Copyright (c) 2014 Sebastian Shelley. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#interface BullsEyeViewController : UIViewController
<UIAlertViewDelegate>
#property (nonatomic, weak) IBOutlet UISlider *slider;
#property (nonatomic, weak) IBOutlet UILabel *targetLabel;
#property (nonatomic, weak) IBOutlet UILabel *scoreLabel;
#property (nonatomic, weak) IBOutlet UILabel *roundLabel;
-(IBAction)showAlert;
-(IBAction)sliderMoved:(UISlider *)slider;
-(IBAction)startOver;
- (IBAction)playSound:(id)sender;
#end
Some help would be greatly appreciated :)
Add an action like this to the button
-(IBAction)myButtonPressed:(id)sender
{
[self playSound:sender];
[self startNewRound:Sender];
}
Use only one action, just set a BOOL to check if you need to play the sound or not.
Example code would be:
-(IBAiction)btnPressed:(id)sender
{
if(playsound)
{
[self playSound];
playsound = NO;
}
[self startOver];
}
And then whenever you want the saund to be played again just set playsound to YES and next time user presses the button it will play the sound again
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
This is probably is pretty easy, but I'm stuck with it today.
The idea is that in my browser, I've create uiwebview and I want to implimate address bar in popover with it own class.
I can get the url from UItextfield from popover class to webview class, but when I get it uiwebview get lazy and it doesn't load it.
When I check it, debuger says that webview is null.
This is ViewController.h
#import <UIKit/UIKit.h>
#import "AdressBar.h"
#import "mypopoverController.h"
#interface ViewController : UIViewController<AddressbarDelegate>
{
UIWebView* mWebView;
mypopoverController *popoverController;
}
#property (nonatomic, retain) IBOutlet UIWebView* webPage;
#end
This is ViewController.m:
#import "mypopoverController.h"
#import "MyOwnPopover.h"
#import "ViewController.h"
#import "AdressBar.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize webPage = mWebView;
- (void)viewDidLoad
{
[super viewDidLoad];
addressBar = [[AdressBar alloc] init];
addressBar.delegate = self;
[edittext addTarget:self action:#selector(showPopoverAdressBar:forEvent:) forControlEvents:UIControlEventTouchUpInside];
NSURL *url = [NSURL URLWithString:#"http://www.google.lv"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[mWebView setScalesPageToFit:YES];
[mWebView setDelegate:self];
[mWebView loadRequest:request];
}
-(void)loadReceivedAddress:(NSURLRequest *)url{
NSLog(#"url= %#", url);//there url always is not null and mWebView should load it
if(mWebView != nil){
[mWebView loadRequest:url];
}else{
NSLog(#"mWebView is null");//...but there it say's that it's null
}}
-(void)showPopoverAdressBar:(id)sender forEvent:(UIEvent*)event
{
AdressBar *popoverControllesr = [[AdressBar alloc]init];
popoverControllesr.view.frame = CGRectMake(0,0, 600, 45);
popoverControllesr.view.backgroundColor = [UIColor whiteColor];
popoverController = [[mypopoverController alloc] initWithContentViewController:popoverControllesr];
popoverController.cornerRadius = 20;
if(_titles!=NULL){
popoverController.titleText = _titles;}else{
popoverController.titleText = #"Loading...";
}
popoverControllesr.address.text = absoluteString;
popoverController.popoverBaseColor = [UIColor orangeColor];
popoverController.popoverGradient= YES;
popoverController.arrowPosition = TSPopoverArrowPositionHorizontal;
[popoverController showPopoverWithTouch:event];
}
#end
This is AdressBar.h
#import <UIKit/UIKit.h>
#protocol AddressbarDelegate <NSObject>
#required
-(void)loadSomethingFromAddressBar:(NSURLRequest*)request;
#end
#interface AdressBar : UIViewController{
IBOutlet UIButton *cancel;
}
#property (nonatomic, retain) IBOutlet UITextField *address;
#property (nonatomic, retain) NSURLRequest *request;
#property(nonatomic, weak) id <AddressbarDelegate> delegate;
#end
This is AdressBar.m:
#import "AdressBar.h"
#import "ViewController.h"
#interface AdressBar ()
#end
#implementation AdressBar
#synthesize delegate = delegate;
- (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 from its nib.
[_address setDelegate:self];
_address.clearButtonMode =
UITextFieldViewModeWhileEditing;
_address.keyboardType = UIKeyboardTypeURL;
[_address addTarget:self
action:#selector(loadAddresss)
forControlEvents:UIControlEventEditingDidEndOnExit];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)loadAddresss {
NSString* urlString = _address.text;
NSURL* url = [NSURL URLWithString:urlString];
if(!url.scheme)
{
NSString* modifiedURLString = [NSString stringWithFormat:#"http://%#", urlString];
url = [NSURL URLWithString:modifiedURLString];
}
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSLog(#"request= %#", request);
NSLog(#"text= %#", urlString);
if (request!=nil) {
if(delegate!=nil)
{NSLog(#"delegate not nil");
[delegate loadSomethingFromAddressBar:request];
}else{
NSLog(#"delegate is nil");//There delegate always is nil
}
}
}
#end
Make sure that your web view Outlet & Delegate is correctly conncted.
There no space between URL Address because I also face the same issue. Try to open that url in web-Browser.
If this is your view hierarchy
---- In ViewController
---- Showing AddressBar View in POPOver
---- Remove POPover and display ViewController's web view
I suggest create a custom delegate method in AddressBar and when you remove the popOver trigger the delegate method. Implement the delegate in your ViewContrller and call loadSomethingFromAddressBar in that implemented delegate method
Note : make sure you have connected you webpage IBOutlet to your nib file.
// In Adressbar.h
#protocol AddressbarDelegate <NSObject>
#required
-(void)loadYourWebViewNow:(NSURLRequest*)request;
#end
#interface Addressbar : UIViewController
{
}
#property(nonatomic, weak) id <AddressbarDelegate>
// In Adressbar.m
- (void)loadAddresss {
NSString* urlString = _address.text; //geting text from UItextField
NSURL* url = [NSURL URLWithString:urlString];
if(!url.scheme)
{
NSString* modifiedURLString = [NSString stringWithFormat:#"http://%#", urlString];
url = [NSURL URLWithString:modifiedURLString];
}
NSURLRequest *request = [NSURLRequest requestWithURL:url];
if (request!=nil) {
NSLog(#"request is good");
if(_delegate!=nil)
{
[_delegate loadYourWebViewNow:request];
}
}
// In your ViewController.h
#interface ViewController : UIViewController<AddressbarDelegate>
{
//AdressBar *addressBar;
}
// In your ViewController.m implement the delegate method and set the delegate
#implementation ViewController
-(void)viewDidLoad
{
Remove these two below lines on viewDidLoad
//addressBar = [[Adressbar alloc] init];
//addressbar.delegate = self;
}
-(void)loadYourWebViewNow:(NSURLRequest*)request
{
[self loadSomethingFromAddressBar:request];
}
-(void)showPopoverAdressBar:(id)sender forEvent:(UIEvent*)event
{
AdressBar *popoverControllesr = [[AdressBar alloc]init];
popoverControllesr.delegate = self; // set the delegate here to this object.
popoverControllesr.view.frame = CGRectMake(0,0, 600, 45);
popoverControllesr.view.backgroundColor = [UIColor whiteColor];
popoverController = [[mypopoverController alloc] initWithContentViewController:popoverControllesr];
popoverController.cornerRadius = 20;
if(_titles!=NULL){
popoverController.titleText = _titles;}else{
popoverController.titleText = #"Loading...";
}
popoverControllesr.address.text = absoluteString;
popoverController.popoverBaseColor = [UIColor orangeColor];
popoverController.popoverGradient= YES;
popoverController.arrowPosition = TSPopoverArrowPositionHorizontal;
[popoverController showPopoverWithTouch:event];
}
I have custom UIButton class:
CheckBox.h
#interface CheckBox : UIButton {
BOOL isChecked;
IBOutlet UIWebView *webview;
IBOutlet UIImageView *img;
NSMutableString *labelText;
NSInteger fontSize;
NSInteger heightWebView;
}
#property (nonatomic,retain) NSMutableString *labelText;
#property (nonatomic,retain) UIImageView *img;
#property (nonatomic,retain) UIWebView *webview;
#property (nonatomic,assign) BOOL isChecked;
-(IBAction) checkBoxClicked;
-(void)addText:(NSString *) text redLetter:(NSInteger)redLetter isBold:(NSInteger)
isBold;
-(BOOL)getStatus;
-(NSString*)getText;
-(void)setFontSize:(NSInteger)setFontSizeValue;
#end
CheckBox.m look on IBAction i need implement functionality there
#import "CheckBox.h"
#implementation CheckBox
#synthesize isChecked, webview, img, labelText, delegate;
- (id)initWithFrame:(CGRect)frame {
if (self == [super initWithFrame:frame]) {
// Initialization code
fontSize = 2;
self.isChecked = NO;
self.labelText = [[NSMutableString alloc] init];
self.contentHorizontalAlignment =
UIControlContentHorizontalAlignmentLeft;
img = [[UIImageView alloc] initWithFrame:CGRectZero];
img.image = [UIImage imageNamed:#"checkbox.png"];
[self addSubview:img];
webview = [[UIWebView alloc] initWithFrame:frame];
webview.backgroundColor = [UIColor clearColor];
[webview setOpaque:NO];
webview.userInteractionEnabled = NO;
[self addSubview:webview];
/* [self setImage:[UIImage imageNamed:
#"checkbox.png"]
forState:UIControlStateNormal];*/
[self addTarget:self action:
#selector(checkBoxClicked)
forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(IBAction) checkBoxClicked{
if(self.isChecked ==NO){
self.isChecked =YES;
img.image = [UIImage imageNamed:#"checkbox-checked.png"];
}else{
self.isChecked =NO;
img.image = [UIImage imageNamed:#"checkbox.png"];
}
}
-(BOOL)getStatus{
return self.isChecked;
}
-(NSString*)getText{
return [NSString stringWithFormat:#"%#",self.labelText];
}
-(void)setFontSize:(NSInteger)setFontSizeValue {
fontSize = setFontSizeValue;
if (fontSize >2) {
heightWebView = fontSize+2;
}
}
-(void)addText:(NSString *) text redLetter:(NSInteger)redLetter isBold:(NSInteger)isBold
{
[self.labelText setString:text];
if (redLetter != 0) {
NSString *first;
NSString *red;
NSString *second;
first = [text substringWithRange:NSMakeRange(0, redLetter-1)];
red = [text substringWithRange:NSMakeRange(redLetter-1, 1)];
second = [text substringWithRange:NSMakeRange(redLetter, [text length] - redLetter )];
if(isBold == 0) {
NSString *html = [NSString stringWithFormat:#"<font size=\"%d\"><p>%#<span style=\"color:red;\">%#</span>%#</p></font>",fontSize, first,red,second];
[webview loadHTMLString:html baseURL:nil];
}else{
NSString *html = [NSString stringWithFormat:#"<font size=\"%d\"><p>%#<span style=\"color:red;\">%#</span>%#</p></font>",fontSize, first,red,second];
[webview loadHTMLString:html baseURL:nil];
}
}else {
if(isBold == 0) {
NSString *html = [NSString stringWithFormat:#"<font size=\"%d\"><p>%#</p></font>",fontSize, text];
[webview loadHTMLString:html baseURL:nil];
}else{
NSString *html = [NSString stringWithFormat:#"<font size=\"%d\"><p>%#</p></font>",fontSize, text];
[webview loadHTMLString:html baseURL:nil];
}
}
}
- (void)layoutSubviews {
img.frame = CGRectMake(0, 5, 18 , 18);
webview.frame = CGRectMake(12, 0-heightWebView, self.bounds.size.width- 11 , 25+heightWebView);
}
- (void)dealloc {
[webview release];
[img release];
[super dealloc];
}
#end
I need to add functionality to this class, that when user click on button in class where i implement CheckBox class will call some void.
Let me explain better i want to implement here functionality like in UIAlertView where you click on button calls
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
I need something like
- (void)checkBox:(CheckBox *)checkBox didStatusChanged:(BOOL)checkBoxStatus
Sounds like you want to implement a delegate protocol. This would go at the top of checkbox.h above your #interface
#protocol CheckBoxDelegate
#optional
- (void)checkBox:(CheckBox *)checkBox didStatusChanged:(BOOL)checkBoxStatus;
#end
You'd then want to add this to your checkbox.h #interface
#property (monatomic, assign) NSObject <CheckBoxDelegate> delegate;
You could then implement the
checkBox:(CheckBox *)checkBox didStatusChanged:(BOOL)checkBoxStatus
function in your ViewController or whatever is creating the checkboxes, and for each checkbox do
[checkbox setDelegate:self];
Then inside -(IBAction) checkBoxClicked you can call
[delegate checkBox:self didStatusChanged:self.isChecked];
and this would call that method on the class spawning the checkboxes/delegate.
Hope this is extensive enough.
Tim