How to simplify code? - function

I'm building an very simple application and I would like to reduce the coding lines in it, and I would like to that by using one or two functions instead of 20.
The app displays 10 buttons. Each button has two buttons to display, both with the same action (sending an email) but each button has a different identity so it also has different email content. It works fine if I do a function for each button, but I know it's possible to simplify that, I just don't know how.
Can someone point me in the right direction? This is what I'm doing right now in my methods:
MFMailComposeViewController *controller1 = [[MFMailComposeViewController alloc] init];
controller1.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[controller1 setToRecipients:[NSArray arrayWithObjects: #"dev#company.com", nil]];
[controller1 setSubject:#"Button 1"];
[controller1 setMessageBody:#"The second option form button 1 was selected" isHTML:NO];
[self presentModalViewController:controller1 animated:YES];
}
[controller1 release];

Set a unique tag (see the tag property) for each button and check the tag of the sender in your one -sendEmail: method. All your buttons call that one method.

Related

Make href hypertext links in JSON strings work in iOS app

I'm using JSON in my app to call elements from a database.
One of these elements is a text block with href links.
The JSON looks like :
"textBlock":"<a href=\"http:\/\/www.website.com\/" target=\"_blank\">Link<\/a>
In my app I call label with :
self.TextLabel.text = self.item[#"textBlock"];
[selfTextLabel sizeToFit];
Result in my app shows :
Link
Would it be possible to write / strip this link properly ?
I came across this solution to strip the html, which works fine, but my links don't work, I would like to know if I can keep my links working.
OK, so after some more searching and trying, I finally got what I needed.
I first tried to put my string in UITextView, selectable with links detection. Would have been great if I had written directly my URLs in the text.
But again, the strings I receive from JSON look like :
Link
I looked at Fancy UILabels and NSDataDetector, but it seemed like the labels were working but still showing http:// which looked not good for me.
So I figured best way was to put this string in a UIWebView, and call it like (I replaced TextLabel in the question with TextView).
[self.TextView loadHTMLString:self.item[#"textBlock"] baseURL:nil];
I finally had some last issue, as the links were opening in the UIWebView instead of Safari.
So I added self.TextView.delegate = self; in viewDidLoad.
And
-(BOOL) webView:(UIWebView *)TextView shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
.h file must also call UIWebViewDelegate.
And if you think UIWebView default font is ugly in this case, like I did, you can do :
NSString *nicerTextBlock = self.item[#"textBlock"];
[self.textView loadHTMLString:[NSString stringWithFormat:#"<style type='text/css'>body { font-family: Helvetica; font-size: 12 } ></style>%#", nicerTextBlock] baseURL:nil];
Hope this can spare some time for other people.

Click-to-call button not working on mobiles

I've created a click-to-call button for use on mobile devices, but I cannot get it to work. It's just a standard button:
<a href="tel:+01845527445" title="Click to call 01845 527 445">
<span>01845 527 445</span>
</a>
The <span> wrapped around the number hides the text number itself, and instead shows a graphic button, which, when pressed, should trigger a call. The graphic button is used at the request of the client.
Can anyone suggest why this isn't working?
Thanks,
Kelly
Your requirement isn't possible to be implemented at the moment.
When you use a webview to make a call, you can't remove the alert as it is the UIWebView behaviour.
If you want, you can use the telprompt:// URL scheme. However, there is a risk that this URL scheme might get removed in future.
NSString *phoneNumber = #"1-545-554-1234";
NSString *phoneURLString = [NSString stringWithFormat:#"tel:%#", phoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
[[UIApplication sharedApplication] openURL:phoneURL];

Flipping through pages on a notebook (HTML5) using Selenium WebDriver (Java)

I want to flip through a notebook, without using any buttons or something like that, but actually clicking on the active element of the page or by dragging the page to get to the next one. The notebook is something similar to this one. I tried many different approaches but failed.
The code I am trying to get to work is:
WebElement page= driver.findElement(By.xpath("//*[#id='pages']/section[4]/div"));
Actions kkk = new Actions(driver);
Actions flip= kkk.moveToElement(page, 780, 200);
flip.click().build().perform();
I also tried the next approach :
flip.perform();
Thread.sleep(200); //to allow the mouse to hover and activate the page
flip.click().perform();
Nothing works, the mouse hovers over the neccesary place and if I try to click in the same place, it just resets and the page rolls back flat.
Also, is there any way finding the neccesarry place (the active spot of the element) without using Offset (coordinates), I can't think of any other way.
This seems more on the lines of drag and drop.
Try doing something like this
WebElement draggable = browser.findElement(By.xpath("//*[#id='pages']/section[4]/div"));
new Actions(browser).dragAndDropBy(draggable, 200, 10).build().perform();
Based on
org.openqa.selenium.interactions.Actions.dragAndDropBy(WebElement source, int xOffset, int yOffset)
UPDATE
or this way
WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();
UPDATE 2
Or this way
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(someElement)
.moveToElement(otherElement)
.release(otherElement)
.build();
dragAndDrop.perform();

Coding a custom navigationItem.rightBarButtonItem once and having it reused in both UITableViewControllers and UIViewControllers?

I have successfully added a rightBarButtonItem to my navigationBar, but I would prefer to only have the code to do so show up once rather than once per type of ViewController. Here's my current setup:
-->TVC
|
NVC--->TVC--->TVC--->VC
So far I've subclassed UITableViewController and moved my code for adding the button into my subclass. All 3 of my TableViewControllers are set to that subclass and it works perfectly.
However now I need my lone ViewController to also show the button, but I don't know how to accomplish this without duplicating the code from my TVC subclass. Is subclassing the right answer or do I need a different approach?
Edits:
#CarlVeazey - Sure, I call it from the viewDidLoad function.
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"BETA" style:UIBarButtonItemStylePlain target:self action:#selector(betaPressed)];
Do a pull-up refactor into a UIViewController category. If your project already has one, just add this code there, otherwise press cmd-N in Xcode to create a new file and choose "Objective-C Category" and enter UIViewController in the "Category On" field.
In the interface add this method declaration:
- (void)onfConfigureRightNavigationBarButton;
And in the implementation add this method implementation:
- (void)onfConfigureRightNavigationBarButton
{
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"BETA"
style:UIBarButtonItemStylePlain
target:self
action:#selector(betaPressed)];
}
Then in any UIViewController subclass you can import your category header and call this method. You may also wish to add to your category an empty implementation of betaPressed:.
BTW, ONF is the prefix I use for non-work coding so use whatever prefix already is in your project, or none at all if you're not concerned with category name collisions.

Adding custom UIView to UIViewController in - (void)loadView (Objective-C)

I am taking a programming class (for noobs) and I need to create the UIViewController graphViewController's view programmatically (without interface builder).
The view is simple, it only consists of an IBOUtlet which is an instance of a UIView subclass called GraphView. graphView responds to several multitouch gestures for zooming and panning and what-not, but I handle all of that stuff in - (void)viewDidLoad.
I am doing just the creation of the self.view property and graphView in the code below:
- (void)loadView
{
UIView *gvcView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = gvcView;
[gvcView release];
GraphView *aGraph = [[GraphView alloc] initWithFrame:self.view.bounds];
self.graphView = aGraph;
[aGraph release];
}
When I run the app I do not see the graphView view in there. I just get a transparent view which shows the "My Universal App" label. I'm stumped. Please help.
Let me know if you need additional code.
Thanks!
Update: Big thanks to BJ Homer for the quick fix!
had to do the following:
add this line of code: [self.view addSubview:self.graphView]; at the end.
I was also getting this strange bug where graphView was showing up as completely black. This line of code fixed that: self.graphView.backgroundColor = [UIColor whiteColor];
And that's it!
Final question: Is the default background color black for a custom UIView?
Thanks again!
[self.view addSubview:self.graphView];
Until you add your graph view to a parent view, UIKit doesn't know where to display it. self.view is special, since that's the property that -loadView is supposed to set. That view will automatically be added to the screen. But your graph view is just floating off in the ether until you add it somewhere.