XCode 4.2: Controlling the Detail View from the Master-Detail Template (iOS 5)

In XCode 4.2 (using the iOS 5 SDK), the Master-Detail Application template has replaced the old Split View Application template. Many of the components such as the RootViewController class and the MainWindow.xib no longer exist. The template is much simpler, but it does take some getting use to. The main problem is to control the DetailViewController from the MasterViewController. When running the template without modifications, the MasterViewController only controls itself, and has no control over the DetailViewController. This is a walkthrough of the process of gaining access to the detail side.

Getting Started with the Master-Detail Application Template

1. Start a new project then select the Master-Detail Application Template.

 

2. For this sample, set the Device Family to iPad and disable the “Use Storyboard”.

 

3. Run the app in your iPad simulator, and you’ll notice when you click the menu, it only affects itself, not changing the large detail view.

 

When looking at the code from MasterViewController.h, there is a pointer called detailViewController, this is not a reference to the large detail view in the split view. Instead, it is a reference to the new menu screen that will be pushed on to of the menu table.

Referencing the DetailViewController

At this point there is no way to change anything in the large DetailViewController. This is how you add that reference:

1. Add the following line to the MasterViewController.h file:

@property (strong, nonatomic) DetailViewController *mainDetailViewController;

 

2. Add the synthesize statement to your MasterViewController.m file:

@synthesize mainDetailViewController;

 

3. Add the reference to mainDetailViewController from AppDelegate.m:

masterViewController.mainDetailViewController = detailViewController;

 

4. Now you can start making changes to the large DetailViewController. Let’s change the wording in the label. In the didSelectRowAtIndexPath function in the MasterViewController.m, add the following line:

mainDetailViewController.detailDescriptionLabel.text = @"I did it!";

 

Now you can run the app, and you will notice that when you press the Detail cell, you will change the text in the main detail view.

Programmatically Attaching an Excel Document to an Email in .NET

Periodically clients will ask me to send them a report from a database. Instead of creating an Excel spreadsheet from the database and then attaching it to an email, I have automated the process. The following function will take in a MailMessage object by reference along with a DataGrid object and filename string. The function then attaches an Excel file containing the DataGrid contents to the MailMessage object.

This function uses the following inclusions: System.Net, System.IO, System.Net.Mail, System.Collections, System.Collections.Generic

    // Function attaches a DataGrid to Email in Excel Format
    private void AttachDataGridToEmail(ref MailMessage mailMessage, DataGrid dataGrid, String filename)
    {
        MemoryStream ms = new MemoryStream();
        System.IO.StringWriter stringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
        dataGrid.RenderControl(htmlWriter);
        byte[] excelFile = StrToByteArray(stringWriter.ToString());
        ms.Write(excelFile, 0, excelFile.Length);
        ms.Position = 0;
        Attachment attachment = new Attachment(ms, filename + ".xls", "application/vnd.xls");
        mailMessage.Attachments.Add(attachment);
    }

MS SQL – “Create failed for User” Error During Database Restore

When migrating a database, from one location to another, you may encounter a problem with the users. The database backup/restore method does not transfer the users. When you try to create the user after the restore process, you will get:

Create failed for User dbusername. (Microsoft.SqlServer.Smo)

Additional information:

An exception occurred while executing a Transact-SQL statement or batch.
(Microsoft.SqlServer.ConnectionInfo)

User, group, or role ‘dbusername‘ already exists in the current database. (Microsoft SQL server, Error: 15023)

This problem is caused by an internal id conflict of the users. It can be corrected by a few lines. For this example, let’s assume your restored database is named EmployeesDB and your user is employeeDBAdmin.

Click on New Query, and type the following.

Use EmployeeDB
exec
sp_change_users_login 'update_one', 'employeeDBAdmin', 'employeeDBAdmin'

Click on ! Execute.

This will synch your users, and you will be able to log into the database as usual.

The full details of this solution can be seen at the Microsoft KB 274188.

Fixing the – warning: UUID mismatch detected with the loaded library

While developing in iOS, I will sometimes get an annoying error while loading apps in different iOS version. The UUID mismatch error looks like this:

UUID mismatch detected

Depending on which version of iOS you are working on, the sections in red and green (below) might differ from mine:

warning: UUID mismatch detected with the loaded library – on disk is:
/Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.3 (8J3)/Symbols/System/Library/Frameworks/CoreGraphics.framework/Resources/libCGFreetype.A.dylib

This problem can be fixed from your terminal. Simply add enter the line below matching the green and red sections from your warning message. You may be required to enter your password.

sudo rm -rf /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.3\ \(8J3\)/

After the command above, unplug your iOS device, restart XCode, and plug your device back in.

If the Organizer window does not open in XCode, go to Window -> Organizer in the top menu. Select the device on the left, and choose Collect Data from the main panel. XCode will gather all the information it needs, and you will be all set to develop on the device.

Thanks to Charles Schmidt from Off Hours Agency for help in discovering this solution.

Providing Alternate Content for Flash Objects with SWFObject

HTML 5 is supposed to take over the Internet sometime in the future, but the predictions are always changing. Until then, Adobe® Flash® remains the easiest way to create interactive animations online. Since Apple mobile devices using iOS, such as iPhone and iPad, are not compatible with Flash, it’s nice to have a detection mechanism to provide alternate content. SWFObject provides a simple and clean solution to your alternate content problem. SWFObject also allows you to add Flash content without all the messy javascript code generated by default during Flash publication.

You can view the example below in my SWFObject Sample Page. If you disable flash in your browser or device, the page will only display a static image. It’s a great way to gracefully degrade your website. You can also use this method to detect older versions of Flash. Believe it or not, some corporations will not allow employees to upgrade their Flash Player.

Here is a simple example of how to use SWFObject:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>SWFObject Sample</title>

	<!-- Including SWFObject from Google -->
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
    
	<script type="text/javascript">
		var params = {
				allowscriptaccess: "always",
				menu: "false"
			};
	
			var flashvars = {};
			var attributes = {};
	
			if (swfobject.hasFlashPlayerVersion("9")) {
				// SWFObject will load "animation.swf" into tag with id of "containerWrapper"
				swfobject.embedSWF("http://www.zugno.com/coderscooler/samples/swfobject-sample/animation.swf", "containerWrapper", 300, 100, "9.0.0", false, flashvars, params, attributes);
			}
	</script>
</head>

<body>
	<div id="containerWrapper">
    	<!-- Contains the content to display if media does not display Flash, or Flash version is below 9.0.0 -->
    	<img src="http://www.zugno.com/coderscooler/samples/swfobject-sample/animation.gif" width="300" height="100" alt="Animation Image" />
    </div>
    <p>Animation will show in Flash (version 9.0.0 or higher) enabled devices, otherwise it will show a static image.</p>
</body>
</html>

For the sake of simplicity, I just added an image to replace the flash, but in your site, you can include HTML in your alternate text. I have also used jQuery to animate the items and to provide interactivity for users without Flash.