Thursday, July 21, 2011

iAdd sample for Monotouch app.

This sample shows how to display a banner Add on a montouch app.
I'm using a simple example to show the use of AdBannerView control.
Create a sample Iphone Navigation-based project from Monodevelop.
In Main.cs/AppDelegate class/FinishedLaunching() method add the following code.

public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
        ADBannerView banner = new ADBannerView(); 
        banner.CurrentContentSizeIdentifier = "ADBannerContentSizePortrait";
        banner.AdLoaded += delegate(object sender, EventArgs e) {
        banner.Hidden = false;
        };
        banner.FailedToReceiveAd += delegate(object sender, AdErrorEventArgs e) {
        banner.Hidden = true;
        };
        banner.Frame = new System.Drawing.RectangleF(0, 30, 320, 50);
        navigationController.Add(banner);
        window.AddSubview (navigationController.View);
        window.MakeKeyAndVisible ();
        return true;
}

The banner must be set to "ADBannerContentSizePortrait" or "ADBannerContentSizeLandscape" using CurrentContentSizeIdentifier property. In this example I'm only targeting portrait layout.
There are two delegates associated with the banner.
FailedToReceiveAd - This is when there is no connection between the app and the iAdd server, the banner is hidden in this case.
AdLoaded - When the iAdd server sends the Add content to display, this is then showed on the Add.




Tuesday, February 1, 2011

Hide Right Bar(EDIT) Button of MoreNavigationController of a UITabBarController using Monotouch

To remove the Edit button from the "More" Navigation controller in a UITabBarController as shown.



First assign a delegate class to the instance of the MoreNavigatioController in the AppDelegate class

tabBarController.MoreNavigationController.Delegate = new MoreNavigationControllerDelegate();


public partial class AppDelegate : UIApplicationDelegate
{
  TabBarController tabBarController;
  // This method is invoked when the application has loaded its UI and its ready to run
  public override bool FinishedLaunching (UIApplication app, NSDictionary options)
  {
     // Create the tabs
      tabBarController = new TabBarController ();
      tabBarController.MoreNavigationController.Delegate = new MoreNavigationControllerDelegate();
   
     // Create the main window and add the navigation controller as a subview
      window = new UIWindow (UIScreen.MainScreen.Bounds);
      window.AddSubview(tabBarController.View);
      window.MakeKeyAndVisible ();
      return true;
  }

  // This method is required in iPhoneOS 3.0
  public override void OnActivated (UIApplication application)
  {
  }
}
 


  • Add a new class "MoreNavigationControllerDelegate" to the project.
  • This class should implement "UINavigationControllerDelegate".
  • Add an override method  "WillShowViewController" to this class - This method is called whenever its time to load the MoreNavigation view controller.
  • Set the RightBarButtonItem to null in this method.
The following code in "WillShowViewController" method will hide "Edit" button in the "More" controller of UITabBarController.

public class MoreNavigationControllerDelegate : UINavigationControllerDelegate
 {
  public MoreNavigationControllerDelegate () : base()
  {
  }
  
  public override void WillShowViewController (UINavigationController navigationController, UIViewController viewController, bool animated)
  {
   if(navigationController.NavigationBar.TopItem.Title == "More")
    navigationController.NavigationBar.TopItem.RightBarButtonItem = null;
  }
 }



Note- I have the condition if(navigationController.NavigationBar.TopItem.Title == "More"), since I still want to show the right bar button when I select the "Motivation", "Tips" and "Glossary" view controller as shown below.